This should be very clear to me, but for some reason it''s not, and I can''t seem to find any examples that help me. My models looks like this: class Product < ActiveRecord::Base has_many :company_products end class CompanyProduct < ActiveRecord::Base belongs_to :product end How do I find all Product records that have a CompanyProduct.company_id of, say 36? I suppose I could do a find_all statement with an include, but it seems there should be a much easier, ready-made ActiveRecord solution rather than creating my very own joins. How would that work? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 20, 2007, at 6:43 PM, MenDAKE wrote:> This should be very clear to me, but for some reason it''s not, and I > can''t seem to find any examples that help me. My models looks like > this: > > class Product < ActiveRecord::Base > has_many :company_products > end > > class CompanyProduct < ActiveRecord::Base > belongs_to :product > end > > How do I find all Product records that have a > CompanyProduct.company_id > of, say 36? > > I suppose I could do a find_all statement with an include, but it > seems > there should be a much easier, ready-made ActiveRecord solution rather > than creating my very own joins. How would that work?Assuming that "CompanyProduct.company_id" means that you just forgot to mention:> class CompanyProduct < ActiveRecord::Base > belongs_to :productbelongs_to :company> endAnd you can add: class Company < ActiveRecord::Base has_many :company_products has_many :products, :through => :company_products end Then it''s just: Company.find(36).products -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ahhh, yes, that sounds like what I need. Thank you. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---