0

I have a model which I don't know how to focus the following issue:

I have been reading the following posts

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
Community
  • 1
  • 1
Mike Norton
  • 71
  • 1
  • 9
  • If company has many users, houses and owners, these three all should have a company_id field. So where is the problem, I don't understand – Ruby Racer Jan 30 '17 at 14:43
  • Yes, all of them they have company_id, I wan to store the company_id in House and Owner where is associated in the User. When the user creates the New House or the New Owner I wan to add this id (company_id) – Mike Norton Jan 30 '17 at 14:50
  • I would say its very unclear what you are asking and you just seem to be generally confused and not have a clear domain model either. What you are looking for is most likely `has_one through:` and `has_many through:` associations. If you can actually include a your models and a bit about the actual problem domain you are trying to solve (is it for example a real estate app or what are you trying to build?) then this question might be answerable. Right now it will most likely get buried or closed. – max Jan 30 '17 at 14:59
  • I added the classes – Mike Norton Jan 30 '17 at 15:17
  • using `before_create` in a model isn't the right approach. That's a method specifically written for controller callbacks. Models have their own system of callbacks (see [activerecord callbacks](http://guides.rubyonrails.org/active_record_callbacks.html)). As for accessing `current_user` in the model, the best way is to pass it as an argument to method calls. – max pleaner Jan 30 '17 at 21:36
  • maxple I read this https://github.com/plataformatec/devise/wiki/How-To:-Add-a-default-role-to-a-User – Mike Norton Jan 30 '17 at 21:40

1 Answers1

0

Considering you have your associations right, in your controller, when a user creates, for example, a house entry:

def create
     @property = current_user.company.properties.new(property_params)
     ....
 end

Where current_user can be replaced by the variable containing the user currently logged on and property_params are the strong params, sent to the controller from your form.

Likewise for owner

Ruby Racer
  • 5,690
  • 1
  • 26
  • 43
  • I am receiving undefined method `properties' for nil:NilClass, the current_user I assume that is comming from devise as default value, if I raw current_user.company_id in the view I see the value that is storaged in the User model – Mike Norton Jan 30 '17 at 19:39
  • Is there a company for the user? – Ruby Racer Jan 30 '17 at 20:59
  • What do you mean? – Mike Norton Jan 30 '17 at 21:41
  • This message means that current user has no company (company is nil). According to your model, there should be "@user.company" or "current_user.company", since user "belongs to :company"... – Ruby Racer Jan 30 '17 at 22:19