2016-03-01 96 views
0

用戶屬於組織,而組織又可以有多個用戶和子組織。你如何找到所有的組織用戶?查找組織中的所有用戶,它是子組織

我有名爲UserOrganization的型號。它們具有以下關係:

在用戶模式:

belongs_to :organization 

在組織模式:

has_many :users 
has_many :child_organizations, class_name: "Organization", foreign_key:"parent_id" 
belongs_to :parent, class_name: "Organization" 

我想找個誰屬的child_organizations所有用戶當前用戶組織。

@users = current_user.organization.child_organizations.users 

它返回此錯誤:根據您的問題的格式

undefined method `users' for Organization::ActiveRecord_Associations_CollectionProxy:0x8f975d0 
+0

您應該編輯您的問題並按照型號 – Prasad

回答

1

我不完全知道它是什麼,你有你的關係進行建模。但是,如果你想找到的人屬於CURRENT_USER的組織child_organizations所有用戶...

這是你可以設置你的人際關係

class User < ActiveRecord::Base 
    belongs_to :organization 
    belongs_to :child_organization 
end 

class Organization < ActiveRecord::Base 
    has_many :child_organizations 
    has_many :users, through: :child_organizations 
end 

class ChildOrganization < ActiveRecord::Base 
    belongs_to :organization 
    has_many :users 
end 

現在你可以使用下面的

@users = current_user.organization.users 
+0

發佈一些示例數據謝謝,這意味着我需要另一個存儲child_organization用戶的模式。 –