Forum: Ruby on Rails Nested resources deletion

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michael R. (Guest)
on 2008-10-04 15:48

I managed to get the complex forms and nested resources described in to work.

But I'm having problems when deleting a sub item.

  def existing_task_attributes=(task_attributes)
    tasks.reject(&:new_record?).each do |task|
      attributes = task_attributes[]
      if attributes
        task.attributes = attributes

the tasks.delete(task) gives me a mysql error: Mysql::Error: Cannot add
or update a child row: a foreign key constraint fails

My main item has several sub items and a sub item belongs to the main
item. In my mysql database, the constraint is set at the sub item table,
pointing to the main item id and on delete => cascade.

Anyone has an idea on what is going on? Why this is giving me an error?
Michael R. (Guest)
on 2008-10-04 20:21
It looks like when the delete action is executed, rails first updates
the sub item id to null instead of deleting it :s
Michael R. (Guest)
on 2008-10-04 20:26
looks like I solved the problem myself. Looks like when you say delete
it sets the id to null. I needed to use destroy.
This topic is locked and can not be replied to.