# Acts\_as\_ordered\_tree

**URL:** <https://www.ruby-forum.com/t/acts-as-ordered-tree/59181>\
**Category:** Rails\
**Created:** [April 30, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181 "2006-04-30T18:02:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvollmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@tvollmer](https://www.ruby-forum.com/u/tvollmer)\
**Post date:** [April 30, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181/1 "2006-04-30T18:02:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tvollmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@tvollmer](https://www.ruby-forum.com/u/tvollmer)\
**Post date:** [May 10, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181/2 "2006-05-10T18:17:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tvollmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@tvollmer](https://www.ruby-forum.com/u/tvollmer)\
**Post date:** [May 10, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181/3 "2006-05-10T18:34:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tvollmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@tvollmer](https://www.ruby-forum.com/u/tvollmer)\
**Post date:** [May 10, 2006, 7:17pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181/4 "2006-05-10T19:17:08Z")

</div>

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](http://script.aculo.us/playground/test/functional/sortable_tree_test.html)

–

Jeff

---

<div class="post-metadata">

**Author:** ![tvollmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@tvollmer](https://www.ruby-forum.com/u/tvollmer)\
**Post date:** [February 20, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/acts-as-ordered-tree/59181/5 "2007-02-20T16:57:42Z")

</div>

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](http://script.aculo.us/playground/test/functional/sortable_tree_test.html)
> 
> –
> 
> Jeff

Here you go 🙂  
[http://www.agilewebdevelopment.com/plugins/acts\_as\_ordered\_tree](http://www.agilewebdevelopment.com/plugins/acts_as_ordered_tree)

- Brian
