I'm stuck on some associations that I think may be more complex than I
originally anticipated. Not really straightforward stuff for my first
Rails app so if you have a chance to point me in the right direction I
would really appreciate it!!
I have PurchaseItems, RecipeItems, and MenuItems.
PurchaseItems can be an Ingredient of RecipeItems and/or MenuItems.
RecipeItems can be an Ingredient of other RecipeItems and/or
I need to track additional Ingredient-specific attributes as well so
it seems that a has_many :through association is required. I can get
simple examples of this working just fine if I remove other parts of
the logic, but am just dying here trying to get it all to work
together. There is clearly self-referential associations at play here
also and (maybe??) polymorphic as well.
I would be completely thrilled if someone who has done this more than
once could help me out and illustrate the appropriate model
Thanks for your time!!
It's hard for us to help you with the associations when you arent
describing what you're trying to make. Im gonna try to guess based on
what you've given:
This is going to be a web shop with products related to cooking. A
bunch of products (in this case ingredients) can be put together as a
recipe. And obviously, since this is a store, a bunch of products can
be put together as an order (purchase) as well. Correct?
What exactly is a "menu_item"?
You need to explain your objects more clearly. Try and explain the
real world problem first, what are the objects in the real world? I
would have expected to see Recipes in there for example, not just
RecipeItems. Also are there no Ingredient objects?
Of course! Thanks very much or your replies, I really appreciate it.
The app is an administrative tool, not a front end webstore. Sorry I
wasn't more clear upfront, Sharagoz. The user will need to be able to
create three types of items in the system via (nested model) forms:
MenuItems, RecipeItems, and PurchaseItems. Ingredients are not a
specific model that a user would have to interact with, as the
RecipeItems and PurchaseItems are the Ingredients of other RecipeItems
or MenuItems. I thought Ingredient would be an important object for
the application as the name of the join model to link these items,
however if another approach is recommended I am not hard set in this
logic of course.
The end result will be similar to many of the 'add task to project/add
questions and answers to survey' nested model forms there are numerous
tutorials for, but the associations just don't seem as
For a MenuItem form the user should be able to add and remove
RecipeItems or PurchaseItems as Ingredients of the MenuItem (with
details like quantity as well that are particular to the join).
For a RecipeItem form the user should be able to add and remove
RecipeItems or PurchaseItems as Ingredients of the RecipeItem (again,
with details like quantity as well that are particular to the join).
PurchaseItems only need to be created as they are at the bottom of the
chain so to speak and don't contain other items.
For example, Flour might be a purchaseitem, Pasta Dough might be a
RecipeItem, and Spaghetti might be a Menuitem. Colin, the RecipeItems
technically are the recipes you might say, as the ingredients are
tacked to them and there will be fields for instructions in the
RecipeItem model as well.
Thanks for your time to help a newb! It's frustrating to not be using
the cooler features of Rails when you're stuck on the model mapping
I still think you are starting off too deep in the implementation and
may not have got the basic models arranged optimally. You have
described what you want your application wants to do in computer
terms but still have not described the real-world situation that you
For example you say "the RecipeItems technically are the recipes you
might say, as the ingredients are tacked to them and there will be
fields for instructions in the RecipeItem model as well". So in the
real world are there recipes? If so what are their attributes? Does
a recipe have Ingredients for example?