# Relating to records in same table

**URL:** <https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139>\
**Category:** Rails\
**Created:** [April 18, 2008, 10:48am UTC](https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139 "2008-04-18T10:48:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [April 18, 2008, 10:48am UTC](https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139/1 "2008-04-18T10:48:49Z")

</div>

I’m in progress of making a little folder system with Ruby on Rails.  
I’ve got a “folders” table. Each “folder” has got an id and a folder\_id.  
The folder\_id is supposed to represent the folders’ parent folder.

How can I tell Rails?

I would like Rails to automaticly delete all subfolders of a folder when  
I delete the folder it self.

Any ideas?

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [April 18, 2008, 11:06am UTC](https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139/2 "2008-04-18T11:06:20Z")

</div>

David T. wrote:

> I’m in progress of making a little folder system with Ruby on Rails.  
> I’ve got a “folders” table. Each “folder” has got an id and a folder\_id.  
> The folder\_id is supposed to represent the folders’ parent folder.
> 
> How can I tell Rails?
> 
> I would like Rails to automaticly delete all subfolders of a folder when  
> I delete the folder it self.
> 
> Any ideas?

acts\_as\_tree ; see  
[http://dev.rubyonrails.org/browser/plugins/acts\_as\_tree](http://dev.rubyonrails.org/browser/plugins/acts_as_tree)

Stephan

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [April 18, 2008, 11:51am UTC](https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139/3 "2008-04-18T11:51:36Z")

</div>

On 18 Apr 2008, at 09:48, David T. wrote:

> I’m in progress of making a little folder system with Ruby on Rails.  
> I’ve got a “folders” table. Each “folder” has got an id and a  
> folder\_id.  
> The folder\_id is supposed to represent the folders’ parent folder.
> 
> How can I tell Rails?

As has been pointed out, acts\_as\_tree does this sort of thing. More  
generally you can do something like

belongs\_to :parent, :foreign\_key =\> ‘folder\_id’, :class\_name =\> ‘Folder’  
has\_many :children :foreign\_key =\> ‘folder\_id’, :class\_name =\> ‘Folder’

> I would like Rails to automaticly delete all subfolders of a folder  
> when  
> I delete the folder it self.  
> You can use the :dependent option to control that

Fred

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [April 18, 2008, 1:28pm UTC](https://www.ruby-forum.com/t/relating-to-records-in-same-table/135139/4 "2008-04-18T13:28:02Z")

</div>

Frederick C. wrote:

> > How can I tell Rails?

> As has been pointed out, acts\_as\_tree does this sort of thing. More  
> generally you can do something like
> 
> belongs\_to :parent, :foreign\_key =\> ‘folder\_id’, :class\_name =\> ‘Folder’  
> has\_many :children :foreign\_key =\> ‘folder\_id’, :class\_name =\> ‘Folder’

Thank you, it works perfectly.

> > I would like Rails to automaticly delete all subfolders of a folder  
> > when  
> > I delete the folder it self.

> You can use the :dependent option to control that

OK, it works too.

Thanks.
