I have a model which I don't know how to focus the following issue:
I have been reading the following posts
- before_create in rails model
- before_create in user model -- set integer based on email (RAILS)
- Access current_user in model
I have a project where I want to manage some properties (Assets). I have Property, Owner, Company and Users.
The House and the Owner are linked in the DB with a FK so 1 Company has N Owners and 1 Company has N Properties.
The model User is linked to Company, so 1 Company has N users.
How can I access the company_id
in the model Users in order to store this ID in the Properties and in the Owner model when the Property and Owner is created?
Do I have to do it in the controller and model?
Owner Class
class Owner < ApplicationRecord
acts_as_paranoid
has_many :property_owners
has_many :properties, :through => :property_owners
belongs_to :company
belongs_to :country
accepts_nested_attributes_for :properties
Property Class
class Property < ApplicationRecord
acts_as_paranoid
has_many :property_owners has_many :owners, :through =>
:property_owners
belongs_to :company belongs_to :country
This is the Company
Class Company < ApplicationRecord
has_many :properties
has_many :owners
And the last one is Users
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
before_create :set_default_role, only: :create
belongs_to :country
belongs_to :company
belongs_to :user_role
accepts_nested_attributes_for :company