开发者

Find by intermediary attribute in a has_many :through in Rails [duplicate]

This question already has answers here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜