Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


ancestry_column creating the problems. #100

deepaktyagi3i36 opened this Issue · 2 comments

2 participants



I am using ancestry gem for navigation and tree structure.

When I am using ancestry column instead of parent_id column then everything fine. But If i want to use parent_id in the model then I face a problem.

My Model code:-

class Category < ActiveRecord::Base
has_many :connections, :dependent => :destroy
has_ancestry :ancestry_column=>"parent_id"
extend FriendlyId
friendly_id :slug , use: :slugged
def normalize_friendly_id(input)
return input.to_s.to_slug.normalize(transliterations: :russian).to_s

in seeds.rb:-

@c00=Category.create(:name => "Hunting",:permalink => "hunting")
@c01=Category.create(:permalink=>"shooting", :name => "Shooting")

@c0=Category.create(:name => "Archery",:permalink => "hunting-archery", :parent_id=>
@c1=Category.create(:name => "Optics",:permalink => "hunting-optiics", :parent_id=>

@c2=Category.create(:name => "Bows",:permalink => "hunting-archery-bows", :parent_id=>
@c3=Category.create(:name => "Bow Tuning",:permalink => "hunting-archery-bows-tuning",:parent_id=>

then parent_id value is not correct for @c2 and @c3 object.
Briefly explanation is as:

1.) above seed create six rows in the categories table.
2.) first two are the root category and then next two will have the parent_id as the id of first record.
3.) But the problem in next two they also have the same parent_id as the 2.) rows have??

what is the reason of it?? all the parent_id for all record have only root id not anyone(sub children) else??
If I simply use ancestry column in place of parent_id then everything fine.


even if i run this query in rails console then parent_id not set as i given it to the query.
Category.create(:name => "Archery",:permalink => "hunting-archery", :parent_id=>6)

a parent exist with id 6 but still this record have not the parent_id equal to 6 but it set parent_id value to first root(record) id?

Please help me this is the bug in ancestry gem.

Please help..



:parent and :parent_id are virtual model attributes used by the ancestry gem. Do not use a column called :parent_id in your database; it doesn't work that way. The column should be called :ancestry (or a different name that is not used directly by the ancestry gem) and of the type :string. Then using :parent_id => 6 should work as expected.


This is also explained in the README by the way. Please read it carefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.