Forum: Ruby on Rails Has_Many - Saving Child Rows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
54a013d627b7da32410f214b2e08581c?d=identicon&s=25 M. Smith (geek001)
on 2005-12-29 18:02
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
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2005-12-29 19:18
(Received via mailing list)
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
This topic is locked and can not be replied to.