Has_Many - Saving Child Rows

I have read extensively on has_many but cannot find an explicit example
on how to do what I need. I am a newbie, but can grasp it if I see it in
a working example. Here is my scenario…

I have a form which I can add a TODO item (patterned after
http://darkhost.mine.nu:81/~vince/rails/tutorial.html). I have the form
working perfectly so I decided to modify it a bit to add more
functionality. So I created a table called TODO_Notifications. This
table is the “child” table and will contain users who were “emailed”
regarding each TODO item.

So, I added a list of check boxes in the form. The check boxes are a
list of emails that I can send the new TODO item to. When I click SAVE
TODO, the app should save the TODO item and then add each user that I
want emailed to the TODO_Notifications table.

What I am having trouble with is getting the TODO id returned so I can
write the child table…

Here is the code I have so far:
##########################################
item = Todo.new # Create a new instance of Todo, so create a new item
item.attributes = @params[“new_item”]

item.todo_notifications << ToDoNotification.new(???):

if item.save # Try to save our item into the database
redirect_to(:action => “list”) # Return to the list page if it
suceeds
else
render_text “Couldn’t add new item” # Print an error message
otherwise
end
##########################################

So, I am just not sure where to put the code to save the child records
AND how I should handle getting the TODO id back…

Any help would be appreciated.

Marty

Assuming your check_box is something like this

check_box(“todo_notification”,user_to_notify.id)

your code would look something like this (there’s a much more concise
way to create the notification and set the attributes, i should know it
but I don’t, I’m sure someone will chime in)

item = Todo.new # Create a new instance of Todo, so create a new item
item.attributes = params[“new_item”]

@users.each do |user|
user_check_value = params[“todo_notification”][user.id.to_s]
if user_check_value
notification = TodoNotification.new
notification.user = user
item.notifications << notification
end
end

if item.save # Try to save our item into the database
redirect_to(:action => “list”) # Return to the list page if it
suceeds
else
render_text “Couldn’t add new item” # Print an error message
otherwise
end