[Rails] ActiveRecord modelling problem

Discussion in 'Ruby' started by Dmitry V. Sabanin, Oct 7, 2004.

  1. 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 ->
    Dmitry V. Sabanin, Oct 7, 2004
    #1
    1. Advertising

  2. 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.mikrocontroller.net - Das Mikrocontroller-Forum
    Andreas Schwarz, Oct 7, 2004
    #2
    1. Advertising

  3. 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 ->
    Dmitry V. Sabanin, Oct 7, 2004
    #3
  4. 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 ->
    Dmitry V. Sabanin, Oct 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Burt
    Replies:
    3
    Views:
    127
    James Britt
    Feb 12, 2005
  2. Brian Takita
    Replies:
    5
    Views:
    350
    Brian Takita
    May 3, 2005
  3. Replies:
    2
    Views:
    119
  4. Xeno Campanoli
    Replies:
    1
    Views:
    125
    Xeno Campanoli
    May 29, 2008
  5. Xeno Campanoli
    Replies:
    1
    Views:
    125
    Xeno Campanoli
    Jun 3, 2008
Loading...

Share This Page