# Problem with 3-tier delete

**URL:** <https://www.ruby-forum.com/t/problem-with-3-tier-delete/181717>\
**Category:** Rails\
**Created:** [January 16, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/problem-with-3-tier-delete/181717 "2010-01-16T22:46:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sultan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@sultan](https://www.ruby-forum.com/u/sultan)\
**Post date:** [January 16, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/problem-with-3-tier-delete/181717/1 "2010-01-16T22:46:09Z")

</div>

When I try to delete a grandfather in a grandfather, father & son  
relationship as in the following, the grandfather and all the  
associated fathers are deleted, but the sons are left as orphans.  
It seems ROR can do a 2-tier cascading delete, but fails on a 3-tier  
cascading delete.

create\_table “galleries”, :force =\> true do |t|  
t.string “name”  
end

create\_table “gallery\_images”, :force =\> true do |t|  
t.integer “gallery\_id”  
end

create\_table “gallery\_image\_profiles”, :force =\> true do |t|  
t.integer “gallery\_image\_id”  
t.integer “profile\_id”  
end

model gallery.rb  
class Gallery \< ActiveRecord::Base  
has\_many :gallery\_images, :dependent =\>:delete\_all  
end

model gallery\_image.rb  
class GalleryImage \< ActiveRecord::Base  
belongs\_to :gallery

```
has_many :gallery_image_profiles, :dependent => :delete_all

```

end

model gallery\_image\_profile.rb  
class GalleryImageProfile \< ActiveRecord::Base  
belongs\_to :gallery\_image  
belongs\_to :profile  
end

controller: galleries\_controller.rb  
class GalleriesController \< ApplicationController  
def destroy  
@gallery = Gallery.find(params[:id])  
@gallery.destroy  
redirect\_to :action =\> ‘list’  
end  
end
