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 -- Posted via http://www.ruby-forum.com/.
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}'' > endI think you''d better use acts_as_nested_set. I ehanced it in a plug in, contact me ito have it. -- Posted via http://www.ruby-forum.com/.
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 > Tilldid you try: class Item< ActiveRecord::Base acts_as_tree :order => "position" acts_as_list :scope => "parent_id" end -- Posted via http://www.ruby-forum.com/.
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 -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of scott Sent: May 10, 2006 9:34 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: acts_as_ordered_tree 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 > Tilldid you try: class Item< ActiveRecord::Base acts_as_tree :order => "position" acts_as_list :scope => "parent_id" end -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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/sortable_tree_test.html > > > -- > > JeffHere you go :) http://www.agilewebdevelopment.com/plugins/acts_as_ordered_tree - Brian -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maybe Matching Threads
- Using set_primary_key breaks acts_as_tree with non-integer column
- acts_as_list / acts_as_tree / acts_as_nested_set - which one
- acts_as_tree & acts_as_list for a single model?
- counter_cache is not looking for children_count with acts_as_tree
- acts_as_tree with acts_as_list