# Newbie needing help on some tricky associations

**URL:** <https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591>\
**Category:** Rails\
**Created:** [March 23, 2010, 6:28am UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591 "2010-03-23T06:28:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [March 23, 2010, 6:28am UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591/1 "2010-03-23T06:28:46Z")

</div>

Hello,  
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  
MenuItems.

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  
associations.

Thanks for your time!!  
-Andy

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [March 23, 2010, 12:12pm UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591/2 "2010-03-23T12:12:57Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [March 23, 2010, 12:22pm UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591/3 "2010-03-23T12:22:18Z")

</div>

On 23 March 2010 05:27, Andy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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?

Colin

Colin

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [March 23, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591/4 "2010-03-23T14:50:58Z")

</div>

On 23 March 2010 12:58, Andy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> however if another approach is recommended I am not hard set in this
> 
> tacked to them and there will be fields for instructions in the  
> RecipeItem model as well.

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  
are modelling.

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?

Colin

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [March 23, 2010, 1:59pm UTC](https://www.ruby-forum.com/t/newbie-needing-help-on-some-tricky-associations/185591/5 "2010-03-23T13:59:28Z")

</div>

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  
straightforward.

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 😉

-Andy
