# Has\_many through. How to form find

**URL:** <https://www.ruby-forum.com/t/has-many-through-how-to-form-find/140682>\
**Category:** Rails\
**Created:** [June 17, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-form-find/140682 "2008-06-17T14:10:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 17, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-form-find/140682/1 "2008-06-17T14:10:36Z")

</div>

I have this tipical has\_many :through scenario:

class Docs \< ActiveRecord::Base  
has\_many :doc\_cats  
has\_many :categories, :through =\> :doc\_cats  
end

class Categories \< ActiveRecord::Base  
has\_many :doc\_cats  
has\_many :docs, :through =\> :doc\_cats  
end

class Doc\_cats \< ActiveRecord::Base  
belongs\_to :docs  
belongs\_to :categories  
end

Now I would like to get all docs which have (belong to) categories 2 and  
4. How to specify find clouse?

@docs = Docs.find(:all, … etc)

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 17, 2008, 3:39pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-form-find/140682/2 "2008-06-17T15:39:28Z")

</div>

i did not tried on console, but is something like this:

@docs = Doc.find(:all, :include =\> [:doc\_cats], :conditions =\>  
‘doc\_cats.category\_id in [2,4]’)

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 18, 2008, 8:37am UTC](https://www.ruby-forum.com/t/has-many-through-how-to-form-find/140682/3 "2008-06-18T08:37:35Z")

</div>

flaubert wrote:

> i did not tried on console, but is something like this:
> 
> @docs = Doc.find(:all, :include =\> [:doc\_cats], :conditions =\>  
> ‘doc\_cats.category\_id in [2,4]’)

Thank you very much. It works.

Of course I had to update table mames.

by  
TheR
