Should be a fairly simple activerecord question

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?

Ahhh, yes, that sounds like what I need. Thank you.

On Apr 20, 2007, at 6:43 PM, MenDAKE wrote:

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 :product
belongs_to :company
end

And 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 B. http://agileconsultingllc.com
[email protected]