# Saving and updating multiple objects

**URL:** <https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323>\
**Category:** Rails\
**Created:** [October 15, 2009, 12:13am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323 "2009-10-15T00:13:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 15, 2009, 12:13am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323/1 "2009-10-15T00:13:00Z")

</div>

Hi All. First to start, please bare with me as I’m really confused on  
all this and have been stuck for about a day now, so I’m not sure if I  
will even be asking the right questions or not, but here goes.

I’m trying to create a Menu object and then assign a group of Day  
objects to it. This part is working fine. Next I want to go and update  
this Menu and all of the Day objects associated with it. I’ve watched  
the railscast on complex forms, so it kind of makes sense to me, but  
there are a few differences here. Each Day object has an associated Meal  
object. The meals are already created, so all I’m trying to do is assign  
a single Meal to a Day, then update the Menu and all of the days at the  
same time.

Here is the code I have. (I have trimmed down a lot of what isn’t needed  
for this, but I can provide more code if it will help).

[http://pastie.org/655276](http://pastie.org/655276)

The actual issue that I’m having is that when I update the menu, then  
click edit again to edit a second time, I now have extra days. Where I  
first started with 20, I will have an extra “x” amount where “x” is the  
number of days that I updated. So if I update 6 days, then now I will  
have 26 days total, but 6 of those days won’t be saved correctly because  
they will be missing some info.

Here are my two debug(params)  
[http://pastie.org/655301](http://pastie.org/655301)  
[http://pastie.org/655304](http://pastie.org/655304)

Thanks for any help,  
~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 15, 2009, 12:15am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323/2 "2009-10-15T00:15:38Z")

</div>

small correction in the controller code, I know the  
@menu.update\_attributes!(params[:menu]) was commented out. That was from  
some testing, I just forgot to uncomment before posting.

Sorry,

~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 15, 2009, 12:51am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323/3 "2009-10-15T00:51:44Z")

</div>

Ok, I’m thinking it might have something to do with the checkboxes. If I  
place a number into the text\_field, and update, then everything seems  
ok. If I check a box, then that’s when it gets all weird.

Anyone see something that I’m not?

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 15, 2009, 2:08am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323/4 "2009-10-15T02:08:22Z")

</div>

On Wed, Oct 14, 2009 at 3:51 PM, Jeremy W. \<  
removed\_email\_address@domain.invalid\> wrote:

> Ok, I’m thinking it might have something to do with the checkboxes. If I  
> place a number into the text\_field, and update, then everything seems  
> ok. If I check a box, then that’s when it gets all weird.
> 
> Anyone see something that I’m not?

Have you tried checking your work in the console?

-Conrad

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [October 15, 2009, 2:29am UTC](https://www.ruby-forum.com/t/saving-and-updating-multiple-objects/177323/5 "2009-10-15T02:29:00Z")

</div>

Solved it! Thanks to a good friend 🙂

For those who have similar issues, look into the  
accepts\_nested\_attributes\_for :your\_object

it works wonders!

class Person \< ActiveRecord::Base  
has\_many :projects  
accepts\_nested\_attributes\_for :projects  
end

\<% form\_for @person, :url =\> { :action =\> “update” } do |person\_form|  
%\>  
…  
\<% person\_form.fields\_for :projects do |project\_fields| %\>  
Delete: \<%= project\_fields.check\_box :\_delete %\>  
\<% end %\>  
\<% end %\>

fields\_for is an iterator too apparently, so you don’t need to run  
through a for loop with the fields\_for inside it.
