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? -- Posted via http://www.ruby-forum.com/.
On 3/31/06, Stephen Gerstacker <stephen@shortround.net> wrote:> > 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060331/56434c3a/attachment.html
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 On 3/31/06, Chris Hall <christopher.k.hall@gmail.com> wrote:> > > On 3/31/06, Stephen Gerstacker <stephen@shortround.net> wrote: > > > > 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? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060331/a4ea2464/attachment.html