# Object.find in HABTM join table?

**URL:** <https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614>\
**Category:** Rails\
**Created:** [July 15, 2010, 10:30am UTC](https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614 "2010-07-15T10:30:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 15, 2010, 10:30am UTC](https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614/1 "2010-07-15T10:30:26Z")

</div>

Good morning,

is it possible to find objects by giving values for the HABTM join table  
only?

Example:  
menu  
id name parents path  
1 abc -1 /  
2 def 1 /def  
3 ghi 1 /ghi  
4 jkl 2,1 /jkl  
5 mno 2 /mno

that means when I go to /abc the menu consists of menu items with the id  
2, 3 and 4. When I go to /def the menu consits of the menu items with  
the id 4 and 5 but as you can see menu items can be shown in many menus  
but exist only once.

So the tables would look like this:  
menus  
id name path  
1 abc /  
2 def /def  
3 ghi /ghi  
4 jkl /jkl  
5 mno /mno

menus\_parents  
menu\_id parent\_id  
2 1  
3 1  
4 1  
4 2  
5 2

So how is it possible to all menu objects that have 1 as parent?  
My current approach is:  
menus = Menu.all  
menus = menus.select{|m| m.parents.include?(1)}

I don’t really like it cause all menu items are loaded from the database  
each and every time. Is there any better solution to this like some  
find…?

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 15, 2010, 12:18pm UTC](https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614/2 "2010-07-15T12:18:03Z")

</div>

On 15 July 2010 09:30, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 3 ghi 1 /ghi  
> id name path  
> 4 1  
> 4 2  
> 5 2
> 
> So how is it possible to all menu objects that have 1 as parent?  
> My current approach is:  
> menus = Menu.all  
> menus = menus.select{|m| m.parents.include?(1)}

Can’t you do  
menus = Menu.find( :all, :include =\> :parents, :conditions =\>  
['parent.id = ?, 1] )  
or something similar.

Or even better, if you have the parent object, then all the menus that  
have that as a parent are parent.menus

Colin

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 15, 2010, 3:22pm UTC](https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614/3 "2010-07-15T15:22:16Z")

</div>

Hey Colin,

thanks but the parent.menus doesn’t work because the habtm is on the  
same table:  
has\_and\_belongs\_to\_many :parents, :class\_name =\> “Menu”,  
:association\_foreign\_key =\> “parent\_id”, :join\_table =\> “menus\_parents”

The other suggestion works just fine, thanks!

This is the working code:  
@menus = Menu.find(:all, :include =\> :parents, :conditions =\>  
[’`menus_parents`.parent\_id = ?’, parent.id])

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 15, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/object-find-in-habtm-join-table/191614/4 "2010-07-15T17:29:34Z")

</div>

On 15 July 2010 14:22, Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey Colin,
> 
> thanks but the parent.menus doesn’t work because the habtm is on the  
> same table:  
> has\_and\_belongs\_to\_many :parents, :class\_name =\> “Menu”,  
> :association\_foreign\_key =\> “parent\_id”, :join\_table =\> “menus\_parents”

I think there may be a better way of doing this. If you made the join  
table a proper table with a model then for Menu you could do  
has\_many children through menus\_parents  
and also  
has\_many parents through menus\_parents  
then you can go both ways more easily.

Colin
