Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierachy not updated correctly when setting parent_id #89

Closed
icedtoast opened this issue Jan 26, 2014 · 2 comments
Closed

Hierachy not updated correctly when setting parent_id #89

icedtoast opened this issue Jan 26, 2014 · 2 comments
Assignees

Comments

@icedtoast
Copy link

Calling depth on child items always returns 0 and ancestry path just returns itself. I expect them to return 1 and [parent, child] respectively.

I have been using collection_select to choose the content of parent_id in my edit views:

<%= f.collection_select :parent_id, @items_for_select, :id, :name, :include_blank => true %>

My controller then updates the items with the following:

def update
  if @item.update(item_params)
  ...
  end
end

def item_params
  params.require(:item).permit(:name, :parent_id)
end

The results of select queries are the following:

sqlite> select * from item_hierarchies;
ancestor_id|descendant_id|generations
3|3|0
4|4|0
5|5|0

sqlite> select id, parent_id from items;
id|parent_id
1|
3|1
4|1
5|1

Am I doing something wrong here? I would expect setting parent_id would correctly update the hierarchy. I am using Rails 4.

@mceachen
Copy link
Collaborator

Let me see if I can make a test that reproduces this issue.

FWIW, I believe all the docs and my tests always add a child to a parent (like root.children << child), so in the mean time, changing you code to do that would be a workaround.

Thanks for opening the issue!

@mceachen
Copy link
Collaborator

I haven't been able to repro this. If you can do a PR with a failing test, I'm happy to look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants