# Nested resources deletion

**URL:** <https://www.ruby-forum.com/t/nested-resources-deletion/150139>\
**Category:** Rails\
**Created:** [October 4, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/nested-resources-deletion/150139 "2008-10-04T13:48:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [October 4, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/nested-resources-deletion/150139/1 "2008-10-04T13:48:44Z")

</div>

Hi,

I managed to get the complex forms and nested resources described in  
[#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3) 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[task.id.to\_s]  
if attributes  
task.attributes = attributes  
else  
tasks.delete(task)  
end  
end  
end

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?

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [October 4, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/nested-resources-deletion/150139/2 "2008-10-04T18:21:50Z")

</div>

It looks like when the delete action is executed, rails first updates  
the sub item id to null instead of deleting it :s

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [October 4, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/nested-resources-deletion/150139/3 "2008-10-04T18:26:26Z")

</div>

looks like I solved the problem myself. Looks like when you say delete  
it sets the id to null. I needed to use destroy.
