Skip to content

Deterministic order problem #179

Open
@burmashave

Description

@burmashave

I added this gem to a Rails project to test it and am having some difficulty with sort order. I have a simple model:

class Question < ActiveRecord::Base
    has_closure_tree order: 'sort_order'
end

Using this seeds.rb file, I create five questions, then append the third, fourth, and fifth records as children of the first, followed by the second:

names = [ "root", "q2", "q3", "q4", "q5" ]
names.each do |name|
    instance_variable_set("@#{name}", Question.create!)
end
@root.append_child(@q3)
@root.append_child(@q4)
@root.append_child(@q5)
@root.append_child(@q2)

I expected to see the second record (q2) appended as the last child of root. When I look at the database, though, I can't make sense of the results. The children appear to be ordered: q3, q2, q4, q5.

id, parent_id, sort_order, created_at, updated_at
1,NULL,0,"2015-10-07 20:14:04.867462","2015-10-07 20:14:04.867462"
2,1,1,"2015-10-07 20:14:05.024568","2015-10-07 20:14:05.779063"
3,1,0,"2015-10-07 20:14:05.134556","2015-10-07 20:14:05.445102"
4,1,2,"2015-10-07 20:14:05.236059","2015-10-07 20:14:05.545712"
5,1,3,"2015-10-07 20:14:05.328957","2015-10-07 20:14:05.670609"

Am I just misunderstanding how this is supposed to work? The test project's repo is here:

https://github.com/burmashave/closure_tree_test

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions