2010-03-25 3 views
0

나는 has_many 사용자를 가진 회사라고하는 모델을 가지고 있으며 사용자는 belongs_to 회사에 속해있다.has_many inheritance

class Company < ActiveRecord::Base 
    has_many :users 
end 

class User < ActiveRecord::Base 
    belongs_to :company 
end 

뭔가 사용자에 속하는 경우 또한 회사에 속해 있습니까?

답변

1

이 경우 has_many :through 연관을 사용해야합니다.

class Comment < ActiveRecord::Base 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    belongs_to :company 
    has_many :comments 
end 

class Company < ActiveRecord::Base 
    has_many :users 
    has_many :comments, :through => :users 
end 

이제 다음을 수행 할 수 있습니다 감지 감사합니다

c = Company.first 
c.users # returns users 
c.comments # returns all the comments made by all the users in the company 
+0

확인합니다. – shaneburgess