# Should be a fairly simple activerecord question.

**URL:** <https://discuss.rubyonrails.org/t/should-be-a-fairly-simple-activerecord-question/11954>\
**Category:** rubyonrails-talk\
**Created:** [April 20, 2007, 11:16pm UTC](https://discuss.rubyonrails.org/t/should-be-a-fairly-simple-activerecord-question/11954 "2007-04-20T23:16:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [April 20, 2007, 11:16pm UTC](https://discuss.rubyonrails.org/t/should-be-a-fairly-simple-activerecord-question/11954/1 "2007-04-20T23:16:38Z")

</div>

> 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 &nbsp;&nbsp;has\_many :company\_products end
> 
> class CompanyProduct \< ActiveRecord::Base &nbsp;&nbsp;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 &nbsp;&nbsp;belongs\_to :product

&nbsp;&nbsp;&nbsp;belongs\_to :company

> end

And you can add:

class Company \< ActiveRecord::Base &nbsp;&nbsp;&nbsp;has\_many :company\_products &nbsp;&nbsp;&nbsp;has\_many :products, :through =\> :company\_products end

Then it's just:

Company.find(36).products

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com
