0

I a model like this:

class Club < ActiveRecord::Base
  belongs_to :organization
  has_many :career_clubs
  has_many :careers, through: :career_clubs
end

Which appropriately gets careers through the table career_clubs. So far this is only using the club's id field and getting all the records in the career_clubs table with that id.

What I actually want this to do, is use both the id and organization_id, so that it gets all the career_clubs with matching id and organization_id.

I know how to scope things using static data like below, but that doesn't seem to work here.

has_many :career_clubs, -> { where active: true }
Tom Prats
  • 7,364
  • 9
  • 47
  • 77
  • possible duplicate of [Rails has\_many with dynamic conditions](http://stackoverflow.com/questions/2462203/rails-has-many-with-dynamic-conditions) – Hugo Tunius Apr 24 '15 at 16:39

1 Answers1

1

This should do what you want

has_many :career_clubs, ->(club) { 
  where(club_id: club.id, organization_id: club.organization_id)
}
Hugo Tunius
  • 2,869
  • 24
  • 32