# Belongs\_to multiple things

**URL:** <https://www.ruby-forum.com/t/belongs-to-multiple-things/175244>\
**Category:** Rails\
**Created:** [September 10, 2009, 5:47am UTC](https://www.ruby-forum.com/t/belongs-to-multiple-things/175244 "2009-09-10T05:47:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 10, 2009, 5:47am UTC](https://www.ruby-forum.com/t/belongs-to-multiple-things/175244/1 "2009-09-10T05:47:25Z")

</div>

I am wondering what the best way to go about this situation is from a  
design perspective:

A has many Cs. A has many Bs. B has many Cs.

Example (not the example from my real app, so hopefully it makes sense).

We have restaurants. Each restaurant has many menus. Each restaurant  
has many dishes. Every dish /must/ belong to a menu, and every dish  
/must/ belong to a restaurant (conceptually). The same dish cannot  
belong to 2 different menus or restaurants (a dish with all the same  
attributes could, but in the DB its a different dish).

As it is often useful to find all given dishes for a given restaurant  
that match some criteria, it is tempting to do this:

class Restaurant  
has\_many :dishes  
has\_many :menus  
end

class Menu  
has\_many :dishes  
end

class Dish  
belongs\_to :restaurant  
belongs\_to :menu  
end

But this seems somehow bad. I know that it makes deletions work  
strangely (the item can be deleted from the restaurant and still found  
on one of its menus, etc. So right now I am doing it like this:

Class Restaurant  
has\_many :menus  
def dishes  
my\_dishes = []  
menus.each do |menu|  
menu.dishes.each do |dish|  
my\_dishes.push dish  
end  
end  
end  
my\_dishes  
end

Class Menu  
has\_many :dishes  
end

Class Dish  
belongs\_to :menu  
end

Which works well enough, but given\_restaurant.dishes now returns an  
array, so I cannot call ActiveRecord methods on it,  
(some\_restaurant.dishes.find(:all, :conditions {:course =\> “entree”}) )  
which is sometimes inconvenient.

It seems like a fairly common scenario to have A has many Bs and B has  
many Cs, and to want to be able to efficiently access the Cs from A, so  
I was wondering if there was a railsier, DRYer way to go about this.  
Any hints/ideas/tutorial links?

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 10, 2009, 7:33am UTC](https://www.ruby-forum.com/t/belongs-to-multiple-things/175244/2 "2009-09-10T07:33:22Z")

</div>

Nick G. wrote:

> We have restaurants. Each restaurant has many menus. Each restaurant  
> has many dishes. Every dish /must/ belong to a menu, and every dish  
> /must/ belong to a restaurant (conceptually). The same dish cannot  
> belong to 2 different menus or restaurants (a dish with all the same  
> attributes could, but in the DB its a different dish).

I could be misreading your design specifications here, but it seems to  
me that restaurants would have many dishes through menus:

class Restaurant  
has\_many :menus, :dependent =\> :destroy  
has\_many :dishes, :through =\> :menus  
end

class Menu  
belongs\_to :restaurant  
has\_many :dishes, :dependent =\> :destroy  
end

class Dish  
belongs\_to :menu  
end

Then you could do things like:  
restaurant.menus  
restaurant.dishes  
menu.dishes  
dish.menu.restaurant

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 10, 2009, 7:52am UTC](https://www.ruby-forum.com/t/belongs-to-multiple-things/175244/3 "2009-09-10T07:52:19Z")

</div>

Precisely what I’m looking for. I thought I must be missing something,  
somehow reading books/tutorials all at once my brain just lets some  
information go, and I couldn’t remember the pertinent info, but I could  
tell my way was not “railsish”.

Sorry for the silly question and thanks for the answer 🙂

Robert W. wrote:

> Nick G. wrote:
> 
> > We have restaurants. Each restaurant has many menus. Each restaurant  
> > has many dishes. Every dish /must/ belong to a menu, and every dish  
> > /must/ belong to a restaurant (conceptually). The same dish cannot  
> > belong to 2 different menus or restaurants (a dish with all the same  
> > attributes could, but in the DB its a different dish).
> 
> I could be misreading your design specifications here, but it seems to  
> me that restaurants would have many dishes through menus:
> 
> class Restaurant  
> has\_many :menus, :dependent =\> :destroy  
> has\_many :dishes, :through =\> :menus  
> end
> 
> class Menu  
> belongs\_to :restaurant  
> has\_many :dishes, :dependent =\> :destroy  
> end
> 
> class Dish  
> belongs\_to :menu  
> end
> 
> Then you could do things like:  
> restaurant.menus  
> restaurant.dishes  
> menu.dishes  
> dish.menu.restaurant

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [September 12, 2009, 12:12am UTC](https://www.ruby-forum.com/t/belongs-to-multiple-things/175244/4 "2009-09-12T00:12:40Z")

</div>

Thanks for asking the question, it helped me understand things better.

On Sep 10, 1:52 am, Nick G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
