Ticket #5 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

Error with SQL query in InstanceMethods::before_create

Reported by: ezwelty@u.washington.edu Assigned to: jcm
Priority: major Milestone: 0.1 with tests
Component: plugin Version: 0.1
Keywords: Cc:

Description

There is a bug that I found in better_nested_set.rb, in the before_create() method.

You have:

def before_create
  maxright = self.class.count_by_sql("SELECT max(\"#{acts_as_nested_set_options[:right_column]}\") FROM "+self.class.table_name)
  # adds the new node to the right of all existing nodes
  self[acts_as_nested_set_options[:left_column]] = maxright+1
  self[acts_as_nested_set_options[:right_column]] = maxright+2
end

However, this creates a SQL-query like so:

SELECT max("rgt") FROM table
(returns "rgt", not a number

What you really want to do is

SELECT max(rgt) FROM tabl
(returns the actual max() of that column)

Fix the line to look like this:

maxright = self.class.count_by_sql("SELECT max(#{acts_as_nested_set_options[:right_column]}) FROM "+self.class.table_name)

I have tested it, and it will work properly. If you don't fix this, you won't be able to add child objects to parents, as it will throw this error, because lft and rgt clash:

ActiveRecord::ActiveRecordError (Impossible move, target node cannot be inside moved tree.):

Change History

08/19/06 22:36:04 changed by anonymous

  • owner changed from somebody to jcm.

08/24/06 17:33:09 changed by jcm

  • status changed from new to closed.
  • resolution set to fixed.

Thks for your report.

In fact I changed it to

maxright = self.class.maximum(acts_as_nested_set_options[:right_column], :conditions => acts_as_nested_set_options[:scope])

to avoid creating SQL.

10/10/06 13:29:42 changed by jcm

  • keywords deleted.
  • version set to 0.1.
  • component changed from component1 to plugin.
  • milestone set to 0.1 with tests.