1
可能重複:
Rails has_many :through Find by Extra Attributes in Join Model通過在的has_many中介屬性查找:通過Rails的
我有以下多對多建立在我的模型:
class Project < ActiveRecord::Base
has_many :projectcollaborations
has_many :partners, :through => :projectcollaborations, :source => :partner
end
class Partner < ActiveRecord::Base
has_many :projectcollaborations
has_many :projects, :through => :projectcollaborations, :source => :project
end
class Projectcollaboration < ActiveRecord::Base
belongs_to :project
belongs_to :partner
end
我可以訪問:
@partner = Partner.first
@partner.projects
@partner.projectcollaborations.find_by_myrole('creator')
....
現在我該如何訪問@partner
的所有項目,在my-to-many關係表中有myrole創建者?
您的關聯和類名請使用正確的大小寫:即'project_collaborations',不'projectcollaborations'和'ProjectCollaboration',而不是'Projectcollaboration'。這使得你的代碼更容易讀取所有涉及的內容。 –
你得到我的問題嗎? –