# Object.find in HABTM join table?

**URL:** <https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960>\
**Category:** rubyonrails-talk\
**Created:** [July 15, 2010, 8:30am UTC](https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960 "2010-07-15T08:30:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [July 15, 2010, 8:30am UTC](https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960/1 "2010-07-15T08: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:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 15, 2010, 10:16am UTC](https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960/2 "2010-07-15T10:16:17Z")

</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)}

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:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [July 15, 2010, 1:22pm UTC](https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960/3 "2010-07-15T13: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:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 15, 2010, 3:27pm UTC](https://discuss.rubyonrails.org/t/object-find-in-habtm-join-table/52960/4 "2010-07-15T15:27:18Z")

</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"

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
