19

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.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204

5 Answers5

36

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.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • I agree with Adriano, this doesn't work. When I do something like `obj.parent`, the query that runs is something like `select objs.* from objs where objs.parent_id = 3` Look right? Well the obj.parent_id isn't 3, but it's id is. The `children` method does work though. – aarona Feb 23 '14 at 11:09
  • I was able to correct your answer by doing this instead: `belongs_to :parent, :class_name => "Category", :foreign_key => :id, :primary_key => :parent_id` – aarona Feb 23 '14 at 11:16
  • @DJTripleThreat You sure you didn't have a `foreign_key` set on your `belongs_to` association or put the foreign key (`belongs_to`) on the parent? The crucial piece of info here is that you're referencing the parent object from a key on the child object, thus when you call `child.parent` you are going to get a query like `SELECT model.* FROM models WHERE id = child.parent_id LIMIT 1`. – coreyward Feb 24 '14 at 00:07
  • @coreyward comments don't really do code justice so I don't want to post anything. I have a feeling the issue could be caused because I'm using refinery which forces you to name your tables like `refinery_some_namespace_plural_noun` and so Rails is expecting `refinery_some_namespace_singular_noun_id` but I name my column just `singular_noun_id`. – aarona Feb 25 '14 at 00:38
3

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
Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
3

You should take a look at the ancestry gem: https://github.com/stefankroes/ancestry

It provides all the functionality you need and is able to get all descendants, siblings, parents, etc with a single SQL query by using a variant of materialized paths so it'll have better performance than the self-joins and acts_as_tree answers above.

pthamm
  • 1,841
  • 1
  • 14
  • 17
2

Category should have many categories, and the foreign key of each category should be the parent_id. So, when you do parent.children it lists all the categories which have parent_id=parent.id.

Have you read on Single Table Inheritance?

TuteC
  • 4,342
  • 30
  • 40
1

Full Article - https://blog.francium.tech/best-practices-for-handling-hierarchical-data-structure-in-ruby-on-rails-b5830c5ea64d

A Simple table

Table Emp
id: Integer
name: String
parent_id: Integer

Associations

app/models/emp.rb
class Emp < ApplicationRecord
  has_many :subs, class_name: 'Emp', foreign_key: :parent_id
  belongs_to :superior, class_name: 'Emp', foreign_key: :parent_id
end

Scope Definition

class Emp < ApplicationRecord
  ----
  ----
  scope :roots, -> { where(parent_id: nil) }
end

Fetching data

def tree_data
  output = []
  Emp.roots.each do |emp|
    output << data(emp)
  end
  output.to_json
end
def data(employee)
  subordinates = []
  unless employee.subs.blank?
    employee.subs.each do |emp|
      subordinates << data(emp)
    end
  end
  {name: employee.name, subordinates: subordinates}
end

Eager Loading

def tree_data
  output = []
  Emp.roots.includes(subs: {subs: {subs: subs}}}.each do |emp|
    output << data(emp)
  end
  output.to_json
end
bragboy
  • 34,892
  • 30
  • 114
  • 171