Quick Overview:
I have an ‘Employee’, some ‘Merchants’ and some ‘Products’. A
‘Merchant’ has many ‘Products’. An ‘Employee’ has multiple ‘Merchants’,
depending on their relationship. For example, the Employee may be the
enrollment contact for one merchant and the support contact for another.
Therefore, my Employee class looks like this:
class Employee < ActiveRecord::Base
has_many :enrollment_merchants, :class_name => “Merchant”,
:foreign_key => :enrollment_employee_id
has_many :support_merchants, :class_name => “Merchant”, :foreign_key
=> :support_employee_id
end
And my Merchant class looks like this:
class Merchant < ActiveRecord::Base
belongs_to :enrollment_employee, :class_name => “Employee”,
:foreign_key => “enrollment_employee_id”
belongs_to :support_employee, :class_name => “Employee”, :foreign_key
=> “support_employee_id”
has_many :products
end
I’d like to use :through in the association so that I can get all of the
enrollment products and all of the support products for the Employee.
Ideally it would look like:
class Employee < ActiveRecord::Base
has_many :enrollment_merchants, :class_name => “Merchant”,
:foreign_key => :enrollment_employee_id
has_many :support_merchants, :class_name => “Merchant”, :foreign_key
=> :support_employee_id
has_many :enrollment_products, :through => :enrollment_merchants
has_many :support_products, :through => :support_merchants
end
But that obviously doesn’t work. Can I make this work somehow?