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 Trasbo 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

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 Cheung 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.