0
我創建瞭如下結構的多對多關係。兩個模型之間存在多個多對多關聯
Class User
has_many :companies, :through => company_admins
has_many :company_admins
end
Class Company
has_many :company_admins
has_many :users, :through => company_admins
end
Class CompanyAdmin
belongs_to :company
belongs_to :user
end
這裏,User
可以被指定爲管理上的許多Company
。
現在我要與User
和Company
創建另一個多對多關係,其中User
可以遵循許多company
。我怎樣才能做到這一點?
我添加
Class User
has_many :companies, :through => company_admins
has_many :companies, :through => followers
has_many :company_admins
has_many :followers
end
Class Company
has_many :company_admins
has_many :followers
has_many :users, :through => company_admins
has_many :users, :through => followers
end
Class Follower
belongs_to :company
belongs_to :user
end
現在,如果我尋找被管理員使用@company.users
它在Follower
表搜索任何一家公司的用戶。 任何人都可以幫助我與協會?
它的工作。我已經接受了答案。 :D – Emu
還有一件事,你已經使用':class_name'。我見過有人使用':source'。你能告訴我區別嗎? – Emu
@Emu其實它應該是'source'。本文詳細解釋http://stackoverflow.com/questions/13611265/rails-difference-between-source-and-class-name-in-models – Pavan