Forum: Ruby on Rails Complex Through Statement

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc9250fe89fd5c9e8cfb19c1469a3f86?d=identicon&s=25 Stephen Gerstacker (gers4302)
on 2006-03-31 17:07
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?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-31 19:08
(Received via mailing list)
On 3/31/06, Stephen Gerstacker <stephen@shortround.net> wrote:
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-31 19:33
(Received via mailing list)
you need to use :source on the :through association

basically the way :through works is that it tries to infer the name of
the
name of the association, but in your case it can't

if you don't use :source, the has_many will try to find an association
called :supported_products in the mechant class, but it can't, there is
only
:products, and that is what :source does, it says, "look for this
association in the join class".

I'm new to :through as well, but this is what it seems to be intended
for.

class Employee < ActiveRecord::Base
  has_many :enrolled_merchants, :class_name => "Merchant", :foreign_key
=>
"enrollment_employee_id"
  has_many :supported_merchants, :class_name => "Merchant", :foreign_key
=>
"support_employee_id"
  has_many :enrolled_products, :through => :enrolled_merchants, :source
=>
:products
  has_many :supported_products, :through => :supported_merchants,
:source =>
:products
end

class Merchant < ActiveRecord::Base
  belongs_to :enrollment_employee, :class_name => "Employee"
  belongs_to :support_employee, :class_name => "Employee"
  has_many :products
end

class Product < ActiveRecord::Base
  belongs_to :merchant
end
Fc9250fe89fd5c9e8cfb19c1469a3f86?d=identicon&s=25 Stephen Gerstacker (gers4302)
on 2006-03-31 23:38
Thanks, I completely missed that at the bottom of the documentation.
That worked like a charm.
This topic is locked and can not be replied to.