Forum: Ruby on Rails acts_as_ordered_tree

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6d1a17725da0c398a24ba3d489f1676a?d=identicon&s=25 Till Vollmer (Guest)
on 2006-04-30 18:02
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
Eb96620826312516bb4a2d6deeee6688?d=identicon&s=25 jcm (Guest)
on 2006-05-10 18:17
Till Vollmer 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.
Eecd295cdec2454c66e2015036a39408?d=identicon&s=25 scott (Guest)
on 2006-05-10 18:34
Till Vollmer 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
6482b6f41bc46fd1b01ca24c8f8c9b50?d=identicon&s=25 Jeff Ward (Guest)
on 2006-05-10 19:17
(Received via mailing list)
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/...


--

Jeff
B461fd82130edbf69b388d9fe12a418e?d=identicon&s=25 Brian Burns (wizard)
on 2007-02-20 16:57
Jeff Ward 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/...
>
>
> --
>
> Jeff

Here you go :)
http://www.agilewebdevelopment.com/plugins/acts_as...

- Brian
This topic is locked and can not be replied to.