# Nested Models not working

**URL:** <https://www.ruby-forum.com/t/nested-models-not-working/191899>\
**Category:** Rails\
**Created:** [July 20, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/nested-models-not-working/191899 "2010-07-20T13:33:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IanJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5ac83/32.png) [@IanJ](https://www.ruby-forum.com/u/IanJ)\
**Post date:** [July 20, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/nested-models-not-working/191899/1 "2010-07-20T13:33:49Z")

</div>

Urgghhh - Newbie help needed to stop me giving up on Rails.

I have 3 nested models.  
Recipe-\>RecipeParts-\>RecipeIngredients (to the ingredients index so  
maybe 4 levels)

class Recipe \< ActiveRecord::Base  
has\_many :recipe\_parts  
accepts\_nested\_attributes\_for :recipe\_parts

class RecipePart \< ActiveRecord::Base  
belongs\_to :recipe  
has\_many :recipe\_ingredients  
has\_many :ingredients, :through =\> :recipe\_ingredients  
accepts\_nested\_attributes\_for :recipe

class RecipeIngredient \< ActiveRecord::Base  
belongs\_to :recipe\_part  
belongs\_to :ingredient

I’m trying to write an edit page for Recipe that includes a list of  
RecipeParts - and within that a list of RecipeIngredients (each as a  
select list)

# Editing recipe

\<% form\_for(@recipe) do |recipe\_form| %\>

\<% recipe\_form.fields\_for :recipe\_parts do |recipe\_part\_form| %\>  
\<% recipe\_part\_form.fields\_for :recipe\_ingredients do |rcp\_ing| %\>

\<%= rcp\_ing.collection\_select( :name,  
@ing\_list\_for\_select,:id, :name) %\>

```
  <% end %>
 <p><%= recipe_part_form.text_field :title %></p>
 <p><%= recipe_part_form.text_field :description %></p>

```

\<% end %\>  
\<%= link\_to “Add Part”, {:controller =\> “recipes”,:action =\>  
“add\_recipe\_part”, :id =\> @recipe.id} %\>  
\<%= recipe\_form.submit “Update” %\>  
\<% end %\>  
\<%= link\_to ‘Show’, @recipe %\> |  
\<%= link\_to ‘Back’, recipes\_path %\>

The multiple RecipeParts list correctly, but within each part I get 1  
selection for the ingredients only, despite the data having a mixture  
of 0-3 ingredients for the various parts.

So - my recipe\_part\_form.fields\_for line doesn’t seem to be iterating  
over the recipe\_ingredients properly , and I can’t work out why - I  
think I’m using the identical structure that I did for the RecipeParts

- which works fine.  
Secondly - the collection\_select isn’t giving me a correct Selected  
item - but this could be due to the first problem perhaps.

Sorry if the post is too long/stupid/wrong but Rails/Ruby is proving  
to be (BY FAR) the hardest and most obtuse environment I’ve tried  
learning in almost 30 years of programming. After almost 3 months of  
online tutorials,books, reading forums etc and experimentation I can’t  
understand just how little I can get working in Rails.

Ian

---

<div class="post-metadata">

**Author:** ![IanJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5ac83/32.png) [@IanJ](https://www.ruby-forum.com/u/IanJ)\
**Post date:** [July 20, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/nested-models-not-working/191899/2 "2010-07-20T14:32:57Z")

</div>

On 20 July 2010 12:29, IanJ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class RecipePart \< ActiveRecord::Base  
> belongs\_to :recipe  
> has\_many :recipe\_ingredients  
> has\_many :ingredients, :through =\> :recipe\_ingredients  
> accepts\_nested\_attributes\_for :recipe

Is that what you meant? Should it be accepts\_nested\_attributes\_for  
:recipe\_ingredients?

Colin

---

<div class="post-metadata">

**Author:** ![IanJ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5ac83/32.png) [@IanJ](https://www.ruby-forum.com/u/IanJ)\
**Post date:** [July 20, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/nested-models-not-working/191899/3 "2010-07-20T14:59:19Z")

</div>

> Is that what you meant? Should it be accepts\_nested\_attributes\_for  
> :recipe\_ingredients?
> 
> Colin

Yes it should be …for :recipe\_ingredients Thank you.  
I guess I haven’t got to grips with what accept\_nested\_attributes\_for  
was doing, or on which side of the relationship it’s supposed to go  
since I had it on both sides.

So - with the first problem solved it took me 30 seconds to solve the  
2nd one - finally I don’t feel entirely stupid.

Thanks Colin.  
Ian
