[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[Rails] ActiveRecord modelling problem

Dmitry V. Sabanin

10/7/2004 10:21:00 AM

Hi!

I'm designing CMS for a corporate use with Rails and I have a problem with
implementing some of my ideas about content structure.
In my plans, I want to have a model called Folder, to which all the relevant
data will be linked. Any folder can be a parent for any other Folder, so they
should create a hierarchical structure like this:
+ Folder 1
`- Folder 2
`- Folder 3
`- Folder 4
`- Folder 5
+ Folder 6
`- Folder 7
.....and so on
And now the problem: how can I do this with ActiveRecord?
I want to follow KISS for now, so `folders` table is like:
+ id
+ parent_id
+ name
+ description
However, I fear that there's no way to do 'has_many :folders' inside Folder
model, because it's not in AR's design(probably there'll be "name-guessing"
issues or someth like that). Any ideas are very, very appreciated!

Thanks in advance,
--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.
Spam Here -> postmaster@sco.com


3 Answers

Andreas Schwarz

10/7/2004 10:43:00 AM

0

Dmitry V. Sabanin wrote:

> I want to follow KISS for now, so `folders` table is like:
> + id
> + parent_id
> + name
> + description
> However, I fear that there's no way to do 'has_many :folders' inside Folder
> model, because it's not in AR's design(probably there'll be "name-guessing"
> issues or someth like that). Any ideas are very, very appreciated!

This is no problem with ActiveRecord:

belongs_to :parent, :class_name => 'Folder', :foreign_key => 'folder_id'
has_many :children, :class_name => 'Folder', :foreign_key => 'folder_id'

--
http://www.mikrocont... - Das Mikrocontroller-Forum

Dmitry V. Sabanin

10/7/2004 11:10:00 AM

0

On Thursday 07 October 2004 18:44, Andreas Schwarz wrote:
> This is no problem with ActiveRecord:
>
> belongs_to :parent, :class_name => 'Folder', :foreign_key => 'folder_id'
> has_many :children, :class_name => 'Folder', :foreign_key => 'folder_id'

Super, but I'm not sure how to use that now. How can I add Folders to a
Folder? I've tried folder.folders << another_folder and folder.childrens <<
another_folder but with no success. Btw, big thanks for your help and quick
reply!

--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.
Spam Here -> postmaster@sco.com


Dmitry V. Sabanin

10/7/2004 11:31:00 AM

0

On Thursday 07 October 2004 19:09, Dmitry V. Sabanin wrote:
> On Thursday 07 October 2004 18:44, Andreas Schwarz wrote:
> > This is no problem with ActiveRecord:
> >
> > belongs_to :parent, :class_name => 'Folder', :foreign_key => 'folder_id'
> > has_many :children, :class_name => 'Folder', :foreign_key => 'folder_id'
>
> Super, but I'm not sure how to use that now. How can I add Folders to a
> Folder? I've tried folder.folders << another_folder and folder.childrens <<
> another_folder but with no success.
Answering my own question with Andreas help from #rubyonrails, you have to use
Folder#children<< to add folder.

--
sdmitry -=- Dmitry V. Sabanin
MuraveyLabs.
Spam Here -> postmaster@sco.com