Saving and updating multiple objects

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

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/655304

Thanks for any help, ~Jeremy

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

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?

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

Solved it! Thanks to a good friend :slight_smile:

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.