Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:28:04+00:00 2026-05-26T22:28:04+00:00

I’m using Rails 3.1. I have a model Tree and a model TreeNode, and

  • 0

I’m using Rails 3.1. I have a model Tree and a model TreeNode, and I’ve set up a has_many/belongs_to association between Tree and TreeNodes.

# The initial models and associations.

class Tree < ActiveRecord::Base
  has_many :tree_nodes
end

class TreeNode < ActiveRecord::Base
  belongs_to :tree
end

I want to add the concept of a root node, which isn’t necessarily the node that was created first. I can’t implicitly determine the root node through created_date, primary key (id), or an order (since there isn’t a concept of order with nodes). I’m trying to wrap my head around how to set up this association in Rails.

I started off by adding a root_node column with a foreign key on the Tree table, but my Active Record association would then be Tree belongs_to Node and Node has_one Tree. This is because the class that has the foreign key should have the “belongs_to” association, and the other class should have the “has_one” association. This doesn’t sound right to me.

# This code didn't work.

class Tree < ActiveRecord::Base
  has_many :script_steps
  belongs_to :root_tree_node, :class => 'TreeNode'
end

class TreeNode < ActiveRecord::Base
  belongs_to :tree
  has_one :tree
end

I also tried creating a join table with has_one :through, but the associations wouldn’t work either.

# This code didn't work.

class Tree < ActiveRecord::Base
  has_many :script_steps
  has_one :root_node, :class => 'TreeNode', :through => :root_tree_node
end

class TreeNode < ActiveRecord::Base
  belongs_to :tree
  has_one :root_tree_node
end

# This represents the join table.
class RootTreeNode < ActiveRecord::Base
  belongs_to :tree
  belongs_to :tree_node
end

What’s the best way to model this relationship generally speaking, and what’s the best way to set up the associations in ActiveRecord? At the end of the day, I want to be able to do something like this.

tree = Tree.create
some_node = tree.tree_nodes.create
another_node = tree.tree_nodes.create

tree.root_node = another_node
tree.save
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:28:04+00:00Added an answer on May 26, 2026 at 10:28 pm

    Hmm… If you trying to get normal tree structure like for example category tree one way to do this in rails is this:

    def Category < ActiveRecord::Base
      belongs_to :parent, :class_name => "Category", :foreign_key => :parent_id
    end
    

    This means that each row has reference to its parent.
    Root node is the one with

    parent_id == nil
    

    Leaf node is when it’s id is never used as parent_id. For more information check out acts_as_tree plugin (not sure if it works with rails 3).

    Other option that you can find more usefull is nested_set (read more here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.