Search code examples
ruby-on-railsrubytreeancestry

Rails, ancestry gem, populating dropdown with two depth level


I installed ancestry gem successfully following instruction from https://github.com/stefankroes/ancestry and my Categories table look like this: Categories(id, category_name, ancestry, created_at, updated_at) enter image description here

I want to make select based on parent->sub-category->sub-sub-category: enter image description here

At this point with code(taken from https://github.com/stefankroes/ancestry/wiki/Creating-a-selectbox-for-a-form-using-ancestry) i can only manage to get following which is not correct. enter image description here


Solution

  • Your data is wrong. To model this structure you should have ancestry column properly filled-in. It should contain whole path from the root node (ids of the elements) sepatared by '/'. For example for Watches it should be 5/13 so as it was displayed under Clothes (Id: 13)