Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a model that has a tree structure

I have categories that are in a tree structure. I am trying to link them together by defining a parent for each one. (I couldn't figure out how to call the property parent so it's just category for now, but it means the parent).

class Category < ActiveRecord::Base

    has_one :category # the parent category

end 

But the relationship ends up the wrong way around.

The getter function is on the child category (correctly) but the category_id is stored on the parent:

parent = Category.create(:name => "parent")
child = Category.create(:name => "child", :category => parent)

parent.id # 1
child.id # 2

child.category_id # nil
parent.category_id # 2

child.category.name # "parent" (!!)

The parent needs to be able to have multiple children so this isn't going to work.

like image 987
Peter Hall Avatar asked Feb 24 '11 19:02

Peter Hall


People also ask

Which model is presented in a tree structure?

A tree structure, tree diagram, or tree model is a way of representing the hierarchical nature of a structure in a graphical form.

What is a tree model?

What are Tree-Based Models? Tree-based models use a decision tree to represent how different input variables can be used to predict a target value. Machine learning uses tree-based models for both classification and regression problems, such as the type of animal or value of a home.

How do you represent a tree structure?

Binary Tree Representation: A tree is represented by a pointer to the topmost node of the tree. If the tree is empty, then the value of the root is NULL. A Tree node contains the following parts. In C, we can represent a tree node using structures.

What is an example of tree structure?

Another example of a tree structure that you probably use every day is a file system. In a file system, directories, or folders, are structured as a tree. Figure 2 illustrates a small part of a Unix file system hierarchy. The file system tree has much in common with the biological classification tree.


2 Answers

What you're looking for is self joins. Check this section of the Rails guide out: http://guides.rubyonrails.org/association_basics.html#self-joins

class Category < ActiveRecord::Base
  has_many :children, class_name: "Category", foreign_key: "parent_id"
  belongs_to :parent, class_name: "Category"
end

Every Category will belong_to a parent, even your parent categories. You can create a single category parent that your highest level categories all belong to, then you can disregard that information in your application.

like image 89
coreyward Avatar answered Sep 21 '22 04:09

coreyward


You can use acts_as_tree gem to achieve this, find below example and link.

https://github.com/amerine/acts_as_tree/tree/master

class Category < ActiveRecord::Base
  include ActsAsTree

  acts_as_tree order: "name"
end

root      = Category.create("name" => "root")
child1    = root.children.create("name" => "child1")
subchild1 = child1.children.create("name" => "subchild1")

root.parent   # => nil
child1.parent # => root
root.children # => [child1]
root.children.first.children.first # => subchild1
like image 34
Rameshwar Vyevhare Avatar answered Sep 21 '22 04:09

Rameshwar Vyevhare