Find by intermediary attribute in a has_many :through in Rails [duplicate]
Possible Duplicate:
Rails has_many :through Find by Extra Attributes in Join Model
I have the following many to many setup in my model:
class Project < ActiveRecord::Base
has_many :projectcollaborations
has_many :partners, :through => :projectcollaborations, :sour开发者_JS百科ce => :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
I can access:
@partner = Partner.first
@partner.projects
@partner.projectcollaborations.find_by_myrole('creator')
....
now how can I access the @partner
's all project having myrole creator in my many-to-many relationship table?
@collaborations = @partner.projectcollaborations.includes(:projects).find_all_by_myrole('creator')
@projects = @collaborations.map &:project
maybe there is another, prettier, railsier way, but this is how i'd do it
精彩评论