[Betternestedset-talk] Multiple Trees in a Table.

Stephen Schor beholdthepanda at gmail.com
Tue Jan 15 11:35:05 EST 2008


Sorry for the double-email but I was looking line 79 of
better_nested_set.rb:
 options[:scope] = %(#{options[:scope].to_s}.nil? ? "#{options[:scope].to_s}
IS NULL" :
 "#{options[:scope].to_s} = \#{#{options[:scope].to_s}}")

This sets options[:scope] to be a string of ruby code, rather than SQL.
For instance, if options[:scope] was :tree_id, the above would evaluate
to:

'tree_id.nil? ? "tree_id IS NULL" : "tree_id = #{tree_id}"'

And this string is then put into acts_as_nested_set_options on line 88.


On Jan 15, 2008 9:39 AM, Stephen Schor <beholdthepanda at gmail.com> wrote:

> Hi Krishna,
>
> Thanks for the quick reply!  I still get a MySQL error.
> My class looks like:
>
> class Study < ActiveRecord::Base
>   acts_as_nested_set :scope => :tree_id
>   ...
> end
>
> Here's a snippit from a (typically frustrating) script/console session
> using an empty table:
>
> >> s1 = Study.new(:name=>'Grandpa', :oid=>123,:tree_id=>1)
> => #<Study:0x23f7814 @new_record=true, @attributes={"name"=>"Grandpa",
> "updated_at"=>nil, "lock_version"=>0, "lft"=>nil, "tree_id"=>1,
> "parent_id"=>nil, "rgt"=>nil, "oid"=>123, "created_at"=>nil}>
>
> >> s1.valid?
> => true
>
> >> s1.save
> ActiveRecord::StatementInvalid: Mysql::Error: #42000You have an error in
> your SQL syntax; check the manual that corresponds to your MySQL server
> version for the right syntax to use near '? ? "tree_id IS NULL" : "tree_id =
> #{tree_id}")' at line 1: SELECT max(rgt) AS max_rgt FROM studies WHERE
> (tree_id.nil? ? "tree_id IS NULL" : "tree_id = #{tree_id}")
>
> -Thanks again,
> Stephen
>
>
>
> On Jan 15, 2008 12:58 AM, Krishna Dole <dontfall at gmail.com > wrote:
>
> > Hi Stephen,
> >
> > You need to populate the tree_id column for every record. Then things
> > should be fine-- let us know if they aren't.
> >
> > Krishna
> >
> > On Jan 14, 2008 2:42 PM, Stephen Schor <beholdthepanda at gmail.com >
> > wrote:
> > > Hi All,
> > >
> > > I'm having trouble configuring a table to be able to accommodate
> > multiple
> > > trees.
> > > I have a feeling that this is a common question but a few creative
> > searches
> > > came up empty.
> > > I tried using http://wiki.rubyonrails.org/rails/pages/BetterNestedSetas a
> > > guide and created
> > > a tree_id column in my table and used  acts_as_nested_set, :scope =>
> > > :tree_id in my model.
> > >
> > > My rspec tests began failing and reporting the MYSQL error below:
> > >
> > > Mysql::Error: #42000You have an error in your SQL syntax; check the
> > > manual that corresponds to your MySQL server version for the right
> > > syntax to use near '? ? "tree_id IS NULL" : "tree_id = #{tree_id}" AND
> > > (parent_id IS NULL))  ORDER B' at line 1: SELECT * FROM studies WHERE
> > > (tree_id.nil? ? "tree_id IS NULL" : "tree_id = #{tree_id}" AND
> > > (parent_id IS NULL))  ORDER BY lft
> > >
> > > Any insight is greatly appreciated.  I guess I'm a bit hazy on what
> > :scope
> > > is used for and how to use it.
> > > I've also played around with making multiple trees without :scope in
> > my
> > > directive and it most of my method calls
> > > ended up looking like I'd expect.  (.root? and .root got weird on me)
> > >
> > > -Thanks!
> > > Stephen
> > >
> > > _______________________________________________
> > > Betternestedset-talk mailing list
> > > Betternestedset-talk at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/betternestedset-talk
> > >
> > >
> > _______________________________________________
> > 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/20080115/65e5b732/attachment-0001.html 


More information about the Betternestedset-talk mailing list