Hi, I''m trying to get the root of a scoped set but all I get is: r=Album.root(:user_id=>4) ArgumentError: Unknown key(s): user_id My code is quite simple: class Album < ActiveRecord::Base acts_as_nested_set :scope => :user end The db has a user_id column and the columns BNS needs (parent_id,lft,rgt). I''m useing the svn trunk of today. Any tips (I hope the bug is between my ears)? Thanks, Peter
Hi Peter, My table contains multiple roots using scope. I''ve never really called .root on the class. I''ve sometimes wanted to find the root of an instance by calling my_instance.root to return the top-most ancestor or my_instance. Also - looking at the comments of .root in the SingletonMethods module - there''s this info... # Returns the single root for the class (or just the first root, if there are several). # Deprecation note: the original acts_as_nested_set allowed roots to have parent_id = 0, # so we currently do the same. This silliness will not be tolerated in future versions, however. I hope this is helpful to you. -Stephen On Fri, Apr 4, 2008 at 1:31 PM, Peter Schrammel <peter.schrammel at gmx.de> wrote:> Hi, > > I''m trying to get the root of a scoped set but all I get is: > > r=Album.root(:user_id=>4) > ArgumentError: Unknown key(s): user_id > > My code is quite simple: > > class Album < ActiveRecord::Base > acts_as_nested_set :scope => :user > end > > The db has a user_id column and the columns BNS needs (parent_id,lft,rgt). > > I''m useing the svn trunk of today. Any tips (I hope the bug is between > my ears)? > > > Thanks, > > Peter > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080404/4825d281/attachment.html
Thanks Stephen but if you look at the code further down: def root(scope = {}) find_in_nested_set(:first, { :conditions => "(#{prefixed_parent_col_name} IS NULL OR #{prefixed_parent_col_name} 0)" }, scope) end So the so called inner_scope is set to the scope I pass. But then somewhere in find_in_nested_set it throws the error...sigh. Nobody ever needed the root of a scope (I have only one per scope)? Stephen Schor schrieb:> Hi Peter, > > My table contains multiple roots using scope. I''ve never really called > .root on the class. > I''ve sometimes wanted to find the root of an instance by calling > my_instance.root to return > the top-most ancestor or my_instance. > > Also - looking at the comments of .root in the SingletonMethods module - > there''s this info... > > # Returns the single root for the class (or just the first root, if > there are several). > # Deprecation note: the original acts_as_nested_set allowed roots to > have parent_id = 0, > # so we currently do the same. This silliness will not be tolerated in > future versions, however. > > I hope this is helpful to you. >
Would you not use.. user.albums.root() ? On Fri, Apr 4, 2008 at 3:31 PM, Peter Schrammel <peter.schrammel at gmx.de> wrote:> Thanks Stephen > > but if you look at the code further down: > def root(scope = {}) > find_in_nested_set(:first, { :conditions => > "(#{prefixed_parent_col_name} IS NULL OR #{prefixed_parent_col_name} > 0)" }, scope) > end > > So the so called inner_scope is set to the scope I pass. But then > somewhere in find_in_nested_set it throws the error...sigh. > > Nobody ever needed the root of a scope (I have only one per scope)? > > > Stephen Schor schrieb: > > Hi Peter, > > > > My table contains multiple roots using scope. I''ve never really called > > .root on the class. > > I''ve sometimes wanted to find the root of an instance by calling > > my_instance.root to return > > the top-most ancestor or my_instance. > > > > Also - looking at the comments of .root in the SingletonMethods module - > > there''s this info... > > > > # Returns the single root for the class (or just the first root, if > > there are several). > > # Deprecation note: the original acts_as_nested_set allowed roots to > > have parent_id = 0, > > # so we currently do the same. This silliness will not be tolerated in > > future versions, however. > > > > I hope this is helpful to you. > > > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080404/e427592c/attachment.html
Quoting Peter Schrammel <peter.schrammel at gmx.de>:> I''m trying to get the root of a scoped set but all I get is: > > r=Album.root(:user_id=>4) > ArgumentError: Unknown key(s): user_id > > My code is quite simple: > > class Album < ActiveRecord::Base > acts_as_nested_set :scope => :user > end > > The db has a user_id column and the columns BNS needs (parent_id,lft,rgt).I think you need to specify 2 things - what the user id is, and that you want the root of that set. In the model where I similarly want the root of someones things, I do the following: def self.find_persons_base_page(person_id) return PersonalPage.find_by_person_id_and_parent_id(person_id, nil) end -- Cynthia Kiser
Thank you all, I already wrote my own #root(user) method (same solution as Cynthia) but I think the docs/lib have a bug here. It says #root(scope={}) will return the (first) root of my set(s). That is right if you don''t use scope but throws an error if you do. My issue was pointing out this bug (although I hoped it was just a mistake by me). Regards Peter I know I should write a test for this....test.write if me.sparetime.have? Cynthia Kiser schrieb:> Quoting Peter Schrammel <peter.schrammel at gmx.de>: >> I''m trying to get the root of a scoped set but all I get is: >> >> r=Album.root(:user_id=>4) >> ArgumentError: Unknown key(s): user_id >> >> My code is quite simple: >> >> class Album < ActiveRecord::Base >> acts_as_nested_set :scope => :user >> end >> >> The db has a user_id column and the columns BNS needs (parent_id,lft,rgt). > > I think you need to specify 2 things - what the user id is, and that > you want the root of that set. In the model where I similarly want the > root of someones things, I do the following: > > def self.find_persons_base_page(person_id) > return PersonalPage.find_by_person_id_and_parent_id(person_id, nil) > end > >