i got dilemma using has_many, :through.
suppose trying model restaurant's menu through rails. , suppose restaurant can have several different kinds of menus, depending on kind of meal.
for instance, have menu meal type breakfast, menu lunch, , forth. , add functionality adding kids menu , adults menu, etc.
clearly, should have menu , food class, need join table these specify different menus , habtm relationship between them.
but how deal meal type?
note meal type belongs_to many foods, belongs 1 menu. feel adding previous join table inefficient.
in example, think solve creating new model, mealtype, , writing food has_many :meal_types, :through meals.
then updating menu model, since menu can have 1 meal type. means have has_one relationship mealtypes each menu. briefly:
foods class:
has_many menus, :through menu_food_join_table has_many mealtypes, :through meals
menu class:
has_many foods, :through menu_food_join_table has_one mealtype
mealtype class:
belongs_to foods, :through meals belongs_to menus
and here arrived @ dilemma: code stinks, @ least me. doesn't seem practical have several tables simple relationship this. , don't think should add mealtype field foods, since foods on several meals (like coke).
neither should add mealtype menus, since want able separate foods meal types (if wanted show foods allowed @ meals, instance).
what in case? must confess don't have lot experience oo design, why came here.
my final solution
here solution chose solve problem:
since real goal generate menus algorithmically, , since in work don't have save of generated menus, destroyed menu model , created meal model.
then added has-many meals relationship food model, through join table. thing in meal database types of meals want able create (breakfast, lunch, dinner, etc). meal has-many foods through same join table.
i still have menu controller , view, use create menufactory.
this menufactory standard factory pattern, takes in number of days want generate menu , specific algorithm restaurant (for instance, restaurant serves plates less 300 calories) , spits out entire menu. rest simple programming.
you seem overcomplicating things. have 1 restaurant has 1 breakfast menu, 1 lunch menu, 1 dinner menu. that's 3 menus - don't think need model type @ all.
so need
menu: has_many menu_items
menuitem: belongs_to menu
i suppose argue menu item possibly on more 1 menu, having breakfast items on lunch menu - if don't need complicated don't.
if you're trying menu more complicated suggests main problem haven't defined problem , you've jumped trying build database represent haven't defined clearly.
Comments
Post a Comment