1

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, :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

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?

Community
  • 1
  • 1
Kamrul Hassan
  • 294
  • 1
  • 2
  • 13
  • 1
    Please use correct casing for your association and class names: i.e `project_collaborations`, not `projectcollaborations` and `ProjectCollaboration`, not `Projectcollaboration`. This makes you code much more readable for all involved. – Ryan Bigg Sep 23 '11 at 14:24

1 Answers1

0
@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

Marian Theisen
  • 6,100
  • 29
  • 39