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?