From richstyles at gmail.com Sun Apr 1 04:09:48 2007 From: richstyles at gmail.com (SB) Date: Sun, 1 Apr 2007 17:09:48 +0900 Subject: [Betternestedset-talk] getting :scope => :root_id to work Message-ID: <5c77452f0704010109o125c201aj71d7ea9532a3180c@mail.gmail.com> First of all thanks for the hard work. I'm trying to get scope to work as follows: class Topic < ActiveRecord::Base acts_as_nested_set :scope => :root_id Unfortunately, making a topic the child of another has no effect on the root_id value. Am I doing something wrong? >> t = Topic.create => #nil, "lft"=>1, "id"=>11, "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>2, "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, @errors=#, @errors={}>> >> tt = Topic.create => #nil, "lft"=>3, "id"=>12, "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>4, "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, @errors=#, @errors={}>> >> t.root => #nil, "lft"=>"1", "id"=>"11", "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>"2", "parent_id"=>nil}> >> tt.move_to_child_of(t) => #nil, "lft"=>"1", "id"=>"11", "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>"4", "parent_id"=>nil}, @starters=nil, @user=nil, @new_record_before_save=true, @new_record=false, @topic_starters=nil, @errors=#, @errors={}>> >> tt => #nil, "lft"=>"2", "id"=>"12", "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>"3", "parent_id"=>"11"}, @starters=nil, @user=nil, @new_record_before_save=true, @new_record=false, @topic_starters=nil, @errors=#, @errors={}>> As you can see the parent_id is set properly but root remains blank. If this is on purpose and there are no performance issues the I'll go with this. If there is a way to make it work, please illustrate. Sam From mark.noten at itfc.be Sun Apr 1 12:45:02 2007 From: mark.noten at itfc.be (Mark Noten) Date: Sun, 01 Apr 2007 18:45:02 +0200 Subject: [Betternestedset-talk] getting :scope => :root_id to work In-Reply-To: <5c77452f0704010109o125c201aj71d7ea9532a3180c@mail.gmail.com> References: <5c77452f0704010109o125c201aj71d7ea9532a3180c@mail.gmail.com> Message-ID: <460FE18E.30606@itfc.be> Hey Sam, I don't think you're doing some wrong. You're just getting the expected behavior of the scope parameter. The scope column will not be filled in by the better nested set API. You should use it to make different trees for an already existing classification. Here's an example how I use it. There are different products for sale and you want to make a group of sales persons (better nested set) for each product, you can say the :scope => :product_id. Kind regards, Mark SB wrote: > First of all thanks for the hard work. > > I'm trying to get scope to work as follows: > > class Topic < ActiveRecord::Base > acts_as_nested_set :scope => :root_id > > > > Unfortunately, making a topic the child of another has no effect on > the root_id value. Am I doing something wrong? > > > >>> t = Topic.create >>> > => #nil, "lft"=>1, "id"=>11, > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>2, > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > @errors=#, > @errors={}>> > >>> tt = Topic.create >>> > => #nil, "lft"=>3, "id"=>12, > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>4, > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > @errors=#, > @errors={}>> > >>> t.root >>> > => #nil, "lft"=>"1", > "id"=>"11", "root_id"=>nil, "content"=>nil, "user_id"=>nil, > "rgt"=>"2", "parent_id"=>nil}> > >>> tt.move_to_child_of(t) >>> > => #nil, > "lft"=>"1", "id"=>"11", "root_id"=>nil, "content"=>nil, > "user_id"=>nil, "rgt"=>"4", "parent_id"=>nil}, @starters=nil, > @user=nil, @new_record_before_save=true, @new_record=false, > @topic_starters=nil, @errors=# @base=#, @errors={}>> > >>> tt >>> > => #nil, > "lft"=>"2", "id"=>"12", "root_id"=>nil, "content"=>nil, > "user_id"=>nil, "rgt"=>"3", "parent_id"=>"11"}, @starters=nil, > @user=nil, @new_record_before_save=true, @new_record=false, > @topic_starters=nil, @errors=# @base=#, @errors={}>> > > As you can see the parent_id is set properly but root remains blank. > If this is on purpose and there are no performance issues the I'll go > with this. If there is a way to make it work, please illustrate. > > Sam > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > > -- Met vriendelijke groet, Mark Noten Software engineer ITFC gcv E-mail: mark.noten at itfc.be GSM: +32 (484) 698 333 From richstyles at gmail.com Sun Apr 1 19:24:57 2007 From: richstyles at gmail.com (SB) Date: Mon, 2 Apr 2007 08:24:57 +0900 Subject: [Betternestedset-talk] getting :scope => :root_id to work In-Reply-To: <460FE18E.30606@itfc.be> References: <5c77452f0704010109o125c201aj71d7ea9532a3180c@mail.gmail.com> <460FE18E.30606@itfc.be> Message-ID: <5c77452f0704011624s660177c4k14180fe4b6dbe9ce@mail.gmail.com> Mark, Thanks for the reply, after digging through the archives some more I found out that you're supposed to define it yourself with a callback or whatever. Still getting acquainted with the plugin but I guess the major benefit is to be able to Product.find(:all, :root_id => 3) and then put that into an array. I'd love to see a concrete example of how it's actually being used with some code snippets. sam On 4/2/07, Mark Noten wrote: > Hey Sam, > > I don't think you're doing some wrong. You're just getting the expected > behavior of the scope parameter. The scope column will not be filled in > by the better nested set API. You should use it to make different trees > for an already existing classification. > > Here's an example how I use it. There are different products for sale > and you want to make a group of sales persons (better nested set) for > each product, you can say the :scope => :product_id. > > Kind regards, > > Mark > > SB wrote: > > First of all thanks for the hard work. > > > > I'm trying to get scope to work as follows: > > > > class Topic < ActiveRecord::Base > > acts_as_nested_set :scope => :root_id > > > > > > > > Unfortunately, making a topic the child of another has no effect on > > the root_id value. Am I doing something wrong? > > > > > > > >>> t = Topic.create > >>> > > => #nil, "lft"=>1, "id"=>11, > > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>2, > > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > > @errors=#, > > @errors={}>> > > > >>> tt = Topic.create > >>> > > => #nil, "lft"=>3, "id"=>12, > > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>4, > > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > > @errors=#, > > @errors={}>> > > > >>> t.root > >>> > > => #nil, "lft"=>"1", > > "id"=>"11", "root_id"=>nil, "content"=>nil, "user_id"=>nil, > > "rgt"=>"2", "parent_id"=>nil}> > > > >>> tt.move_to_child_of(t) > >>> > > => #nil, > > "lft"=>"1", "id"=>"11", "root_id"=>nil, "content"=>nil, > > "user_id"=>nil, "rgt"=>"4", "parent_id"=>nil}, @starters=nil, > > @user=nil, @new_record_before_save=true, @new_record=false, > > @topic_starters=nil, @errors=# > @base=#, @errors={}>> > > > >>> tt > >>> > > => #nil, > > "lft"=>"2", "id"=>"12", "root_id"=>nil, "content"=>nil, > > "user_id"=>nil, "rgt"=>"3", "parent_id"=>"11"}, @starters=nil, > > @user=nil, @new_record_before_save=true, @new_record=false, > > @topic_starters=nil, @errors=# > @base=#, @errors={}>> > > > > As you can see the parent_id is set properly but root remains blank. > > If this is on purpose and there are no performance issues the I'll go > > with this. If there is a way to make it work, please illustrate. > > > > Sam > > _______________________________________________ > > Betternestedset-talk mailing list > > Betternestedset-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > > > > > > > > -- > Met vriendelijke groet, > > Mark Noten > Software engineer > ITFC gcv > E-mail: mark.noten at itfc.be > GSM: +32 (484) 698 333 > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > -- http://baron.vc/ From richstyles at gmail.com Sun Apr 1 19:48:29 2007 From: richstyles at gmail.com (SB) Date: Mon, 2 Apr 2007 08:48:29 +0900 Subject: [Betternestedset-talk] Turning an Array of Array into Nested Lists Message-ID: <5c77452f0704011648o116a18cekb2d274ede2357aa1@mail.gmail.com> By way of introduction, I want to use betternestedset to generate nested comments. I figured out the generation of html tags and simply needed a way to produce an array of arrays to turn into links. Thought I'd share some useful code snippets that can maybe be improved or used by people here. Here's a code snippet I use to generate a nested
    or
      from an array of arrays. The main difficulty I have is not being able to pass it a cycle method to create alternating styles without hard-coding it into the method (I'd love to know if someone can show me a clean way to do it). In application helper: def html_list(type, elements, options = {}, &block) items = elements.map do |element| if element.is_a?(Array) element = html_list(type, element, options, &block) else element = block.call(element) if block content_tag("li", element) end end content_tag(type, items, options) end def ul(*args, &block) html_list("ul", *args, &block) end def ol(*args, &block) html_list("ol", *args, &block) end In your views (this will be a proper nested list): <%= ul(["first", "first", ["second", ["third", "third", "third"], "second", "second", ["third", "third", "third", ["fourth", "fourth", "fourth"]]]]) {|x| link_to x, :controller => "account", :action => "signup" }%> As you can see, passing a block will help you format the objects. I haven't yet tested this with objects but it should work in one form or another. Any other examples used by members of this list would be appreciated. Sam From mark.noten at itfc.be Mon Apr 2 08:12:53 2007 From: mark.noten at itfc.be (Mark.noten) Date: Mon, 2 Apr 2007 14:12:53 +0200 Subject: [Betternestedset-talk] getting :scope => :root_id to work Message-ID: <01c0800944ca99752b661cb29f408956@217.66.0.26> Hey Sam, the thing is that for me the products are already defined up front before creating a node (a sales person). A product is just a "simple" Rails generated model that doesn't depend on the nested set API. When an administrator creates a new sales person, he or she first selects the product(_id) from a select box and then a manager (a sales person too) for this sales agent (if any). The model of my sales person just has a :belongs_to :product and a betternestedset :scope => product_id. So I actually don't need any callbacks. Kind regards, Mark --------- Original Message -------- From: betternestedset-talk at rubyforge.org To: betternestedset-talk at rubyforge.org Subject: Re: [Betternestedset-talk] getting :scope => :root_id to work Date: 02/04/07 02:32 > Mark, > Thanks for the reply, after digging through the archives some more I > found out that you're supposed to define it yourself with a callback > or whatever. Still getting acquainted with the plugin but I guess the > major benefit is to be able to Product.find(:all, :root_id => 3) and > then put that into an array. > > I'd love to see a concrete example of how it's actually being used > with some code snippets. > > sam > > > > > > > > On 4/2/07, Mark Noten <mark.noten at itfc.be> wrote: > > Hey Sam, > > > > I don't think you're doing some wrong. You're just getting the expected > > behavior of the scope parameter. The scope column will not be filled in > > by the better nested set API. You should use it to make different trees > > for an already existing classification. > > > > Here's an example how I use it. There are different products for sale > > and you want to make a group of sales persons (better nested set) for > > each product, you can say the :scope => :product_id. > > > > Kind regards, > > > > Mark > > > > SB wrote: > > > First of all thanks for the hard work. > > > > > > I'm trying to get scope to work as follows: > > > > > > class Topic < ActiveRecord::Base > > > acts_as_nested_set :scope => :root_id > > > > > > > > > > > > Unfortunately, making a topic the child of another has no effect on > > > the root_id value. Am I doing something wrong? > > > > > > > > > > > >>> t = Topic.create > > >>> > > > => #<Topic:0x310fbf0 @attributes={"title"=>nil, "lft"=>1, "id"=>11, > > > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>2, > > > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > > > @errors=#<ActiveRecord::Errors:0x310f204 @base=#<Topic:0x310fbf0 ...>, > > > @errors={}>> > > > > > >>> tt = Topic.create > > >>> > > > => #<Topic:0x3105d80 @attributes={"title"=>nil, "lft"=>3, "id"=>12, > > > "root_id"=>nil, "content"=>nil, "user_id"=>nil, "rgt"=>4, > > > "parent_id"=>nil}, @new_record_before_save=true, @new_record=false, > > > @errors=#<ActiveRecord::Errors:0x31055c4 @base=#<Topic:0x3105d80 ...>, > > > @errors={}>> > > > > > >>> t.root > > >>> > > > => #<Topic:0x30fed8c @attributes={"title"=>nil, "lft"=>"1", > > > "id"=>"11", "root_id"=>nil, "content"=>nil, "user_id"=>nil, > > > "rgt"=>"2", "parent_id"=>nil}> > > > > > >>> tt.move_to_child_of(t) > > >>> > > > => #<Topic:0x310fbf0 @communities=nil, @attributes={"title"=>nil, > > > "lft"=>"1", "id"=>"11", "root_id"=>nil, "content"=>nil, > > > "user_id"=>nil, "rgt"=>"4", "parent_id"=>nil}, @starters=nil, > > > @user=nil, @new_record_before_save=true, @new_record=false, > > > @topic_starters=nil, @errors=#<ActiveRecord::Errors:0x310f204 > > > @base=#<Topic:0x310fbf0 ...>, @errors={}>> > > > > > >>> tt > > >>> > > > => #<Topic:0x3105d80 @communities=nil, @attributes={"title"=>nil, > > > "lft"=>"2", "id"=>"12", "root_id"=>nil, "content"=>nil, > > > "user_id"=>nil, "rgt"=>"3", "parent_id"=>"11"}, @starters=nil, > > > @user=nil, @new_record_before_save=true, @new_record=false, > > > @topic_starters=nil, @errors=#<ActiveRecord::Errors:0x31055c4 > > > @base=#<Topic:0x3105d80 ...>, @errors={}>> > > > > > > As you can see the parent_id is set properly but root remains blank. > > > If this is on purpose and there are no performance issues the I'll go > > > with this. If there is a way to make it work, please illustrate. > > > > > > Sam > > > _______________________________________________ > > > Betternestedset-talk mailing list > > > Betternestedset-talk at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > > > > > > > > > > > > > > -- > > Met vriendelijke groet, > > > > Mark Noten > > Software engineer > > ITFC gcv > > E-mail: mark.noten at itfc.be > > GSM: +32 (484) 698 333 > > > > _______________________________________________ > > Betternestedset-talk mailing list > > Betternestedset-talk at rubyforge.org > > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > > > > -- > http://baron.vc/ > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > > ________________________________________________ Message sent using UebiMiau 2.7.10 From dontfall at gmail.com Wed Apr 4 10:48:05 2007 From: dontfall at gmail.com (Krishna Dole) Date: Wed, 4 Apr 2007 10:48:05 -0400 Subject: [Betternestedset-talk] MySQL optimisation in move_to In-Reply-To: References: Message-ID: <8d64b97d0704040748t4a8bad9dv93c97d1bd21bacee@mail.gmail.com> Hi Lindsay, Thanks for passing that along. At this point it looks like you're the only one working with significantly large tables, but I'm sure others will benefit from what you've discovered. I've opened a ticket on Trac for this to make sure it doesn't get buried. I'm curious: since you've worked with large tables, what indexing setup do you find gives the best performance with nested sets? A single index on (lft, rgt), or something else? Krishna On 3/28/07, Lindsay Evans wrote: > oops, that second line should read: > > base_set_class.update_all([existing SQL], scope_condition + > update_scope_condition > > I need more coffee :p > > -- > Lindsay Evans > http://lindsayevans.com/ > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > From lindsaye at gmail.com Wed Apr 4 20:14:30 2007 From: lindsaye at gmail.com (Lindsay Evans) Date: Thu, 5 Apr 2007 10:14:30 +1000 Subject: [Betternestedset-talk] MySQL optimisation in move_to In-Reply-To: <8d64b97d0704040748t4a8bad9dv93c97d1bd21bacee@mail.gmail.com> References: <8d64b97d0704040748t4a8bad9dv93c97d1bd21bacee@mail.gmail.com> Message-ID: Hi Krishna, I've mostly been using children and all_children, so I'm only indexing lft & parent_id at the moment, just did a quick test with an index on (lft, rgt) and the speed seems comparable to just using lft. >From a quick look at the code, it looks like (lft, rgt) would be best for things like self_and_ancestors and the move_to_* methods I'm no expert on indexes, but I imagine (lft, rgt) would slow down updates on lft & rgt a bit, so there might not be any performance gains for move_to_*. One of these days I'll get motivated & test all these other methods out to see how they go on ridiculously large tables :) (btw, I'm a he, not a her - heaps of people make that mistake :p) On 4/5/07, Krishna Dole wrote: > Hi Lindsay, > > Thanks for passing that along. At this point it looks like you're the > only one working with significantly large tables, but I'm sure others > will benefit from what you've discovered. I've opened a ticket on Trac > for this to make sure it doesn't get buried. > > I'm curious: since you've worked with large tables, what indexing > setup do you find gives the best performance with nested sets? A > single index on (lft, rgt), or something else? -- Lindsay Evans http://lindsayevans.com/ From lindsaye at gmail.com Wed Apr 4 20:52:56 2007 From: lindsaye at gmail.com (Lindsay Evans) Date: Thu, 5 Apr 2007 10:52:56 +1000 Subject: [Betternestedset-talk] MySQL optimisation in move_to In-Reply-To: References: <8d64b97d0704040748t4a8bad9dv93c97d1bd21bacee@mail.gmail.com> Message-ID: Okay, I couldn't help myself & ran a few tests with different indexes, looks like an index on (rgt,lft) would be better than (lft,rgt) in those cases. -- Lindsay Evans http://lindsayevans.com/ From tekin at raid.nu Wed Apr 25 12:35:37 2007 From: tekin at raid.nu (Tekin Suleyman) Date: Wed, 25 Apr 2007 17:35:37 +0100 Subject: [Betternestedset-talk] indexing Message-ID: <000a01c78757$c1ff7f80$0500000a@rude10> Hi. I've just started using BetterNestedSet and have a quick question about indexing. Other than indexes on lft and rgt, are there any other indexes which would improve the performance of my queries? Currently using it to organise categories in a tree structure. Thanks! Tekin From dontfall at gmail.com Wed Apr 25 13:35:22 2007 From: dontfall at gmail.com (Krishna Dole) Date: Wed, 25 Apr 2007 13:35:22 -0400 Subject: [Betternestedset-talk] indexing In-Reply-To: <000a01c78757$c1ff7f80$0500000a@rude10> References: <000a01c78757$c1ff7f80$0500000a@rude10> Message-ID: <8d64b97d0704251035l12e5116cn2d7b313e5f6e222@mail.gmail.com> Hi Tekin, Welcome to the list. There is currently no authoritative answer to the question of indexing, but Al Evans reports that "Working with simulated large databases (with InnoDB tables), my experience is that adding indexes on lft and/or rgt only slows things down. The only index that helps is one on parent_id." My advice would be to just keep an eye out for slow queries and then try different index setups if you spot a problem. Krishna On 4/25/07, Tekin Suleyman wrote: > Hi. > I've just started using BetterNestedSet and have a quick question about > indexing. Other than indexes on lft and rgt, are there any other indexes > which would improve the performance of my queries? Currently using it to > organise categories in a tree structure. > Thanks! > > Tekin > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk > From jc.michel at symetrie.com Sun Apr 29 17:54:48 2007 From: jc.michel at symetrie.com (Jean-Christophe Michel) Date: Sun, 29 Apr 2007 23:54:48 +0200 Subject: [Betternestedset-talk] [Rails] better nested set and :through In-Reply-To: References: Message-ID: <52dd2649fd7caa6d5a587192d8f54cd3@symetrie.com> Hi, Le 29 avr. 07, ? 14:54, Vincent AE Scott a ?crit : > It's been a while since i've been doing any rails development, so i'm a > bit rusty. Having found this: > http://opensource.symetrie.com/api/better_nested_set/ > > it looks really interesting and useful for solving something i'm trying > to get working. > > What i have (essentially) is the following models: > > class Group < ActiveRecord::Base > acts_as_nested_set > has_many :memberships > has_many :users, :through => :memberships > belongs_to :owner, :class_name => "User" > end > > class Membership < ActiveRecord::Base > belongs_to :group > belongs_to :user > end > > class User < ActiveRecord::Base > has_many :memberships > has_many :groups, :through => :memberships > > end > > Now then, what i'd like to know is how do i go about finding all the > Users contained within a nested set? > > for example: > > admins = Group.find_by_name "admins" > admins.users # gives all users in the admins group. > > and > > admins.all_children # gives me all the groups > > I thought the following methods sounded promising, but i get > NoMethodError's from them: > > admins.children.find_all_in_users > admins.children.find_in_users > > > Perhaps i'm going about this the wrong way or i'm missing a more > obvious > solution. Can anyone shed some light on it? I think the problem you raise is more related to find and :through than to nested sets. You should have a look on :include in find : admins = Group.find_by_name 'admins', :include => [{'memberships' => 'users'}] then simply grab your users with users = admins.inject([]){|u, a| u << a.users}.uniq Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set