Acts_as_ordered_tree


#1

I need an ordered (sortable tree) and I just was thinking if somehting
like this is possible:

class Item< ActiveRecord::Base
acts_as_tree :order => “position”
acts_as_list :scope => ‘parent_id = #{self.id}’
end

For my understanding mixing of two acts_as should be póssible, at least
in this case, right?

Unfortunatly the :scope is not working, I am not sure how to write the
scope so that only items are treated as list whos parent_id is like
mine.
I tried:
‘parent_id = #{self.id}’
‘parent_id = #{id}’

But I always get empty SQL statements like:

ORDER BY position DESC LIMIT 1’ at line 1: SELECT * FROM ideas WHERE
(parent_id = ) ORDER BY position DESC LIMIT

Is it possible as I try or do I have to write my own
acts_as_sortable_tree?

Regards
Till


#2

Till V. wrote:

I need an ordered (sortable tree) and I just was thinking if somehting
like this is possible:

class Item< ActiveRecord::Base
acts_as_tree :order => “position”
acts_as_list :scope => ‘parent_id = #{self.id}’
end

I think you’d better use acts_as_nested_set.
I ehanced it in a plug in, contact me ito have it.


#3

Till V. wrote:

I need an ordered (sortable tree) and I just was thinking if somehting
like this is possible:

class Item< ActiveRecord::Base
acts_as_tree :order => “position”
acts_as_list :scope => ‘parent_id = #{self.id}’
end

For my understanding mixing of two acts_as should be póssible, at least
in this case, right?

Unfortunatly the :scope is not working, I am not sure how to write the
scope so that only items are treated as list whos parent_id is like
mine.
I tried:
‘parent_id = #{self.id}’
‘parent_id = #{id}’

But I always get empty SQL statements like:

ORDER BY position DESC LIMIT 1’ at line 1: SELECT * FROM ideas WHERE
(parent_id = ) ORDER BY position DESC LIMIT

Is it possible as I try or do I have to write my own
acts_as_sortable_tree?

Regards
Till

did you try:

class Item< ActiveRecord::Base
acts_as_tree :order => “position”
acts_as_list :scope => “parent_id”
end


#4

Yes, this works great for me…

class Page < ActiveRecord::Base
acts_as_tree :order => :position
acts_as_list :scope => :parent_id
end

This assumes that you have a “parent_id” and “position” column in your
“pages” table.

You can then use the dot notation to select a page’s parent, root,
siblings
or childs such as…

page = Page.find(123)

page.parent.id
page.root.id
page.children.first.id

AND, if you do a page.destroy … all of its sub-nodes get destroyed
too.

… that covers the functionality of the acts_as_tree but now you also
get
the flexibility of the acts_as_list…

page.move_higher
page.move_lower
page.move_to_bottom
page.move_to_top

and because your scope is the model’s very own “parent_id” … your
‘list’
will be confined within each parent_id and won’t mess with the rest of
the
table.

What I haven’t figured out is an elegant way to say “get all root nodes
(parent ID of 0)” and maintain their “position”. I found some
.find_by_parent_id(nil) function somewhere but it didn’t work (and I
can’t
re-find it to show you the reference).

If anybody has a suggestion other than:

@nodes = Page.find(:all,
:conditions => “parent_id = 0”,
:order => “position asc”)

AND, If anybody would know how to implement this in Rails, please do
share!

http://script.aculo.us/playground/test/functional/sortable_tree_test.html

Jeff


#5

Jeff W. wrote:

What I haven’t figured out is an elegant way to say “get all root nodes
(parent ID of 0)” and maintain their “position”. I found some
.find_by_parent_id(nil) function somewhere but it didn’t work (and I
can’t
re-find it to show you the reference).

If anybody has a suggestion other than:

@nodes = Page.find(:all,
:conditions => “parent_id = 0”,
:order => “position asc”)

AND, If anybody would know how to implement this in Rails, please do
share!

http://script.aculo.us/playground/test/functional/sortable_tree_test.html

Jeff

Here you go :slight_smile:
http://www.agilewebdevelopment.com/plugins/acts_as_ordered_tree

  • Brian