# Has\_Many - Saving Child Rows

**URL:** <https://www.ruby-forum.com/t/has-many-saving-child-rows/51323>\
**Category:** Rails\
**Created:** [December 29, 2005, 6:02pm UTC](https://www.ruby-forum.com/t/has-many-saving-child-rows/51323 "2005-12-29T18:02:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![geek001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@geek001](https://www.ruby-forum.com/u/geek001)\
**Post date:** [December 29, 2005, 6:02pm UTC](https://www.ruby-forum.com/t/has-many-saving-child-rows/51323/1 "2005-12-29T18:02:04Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![geek001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@geek001](https://www.ruby-forum.com/u/geek001)\
**Post date:** [December 29, 2005, 7:18pm UTC](https://www.ruby-forum.com/t/has-many-saving-child-rows/51323/2 "2005-12-29T19:18:12Z")

</div>

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
