Relating to records in same table

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?

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

Stephan

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs