Ticket #17 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

SQLIte3 Compatibility with better_nested_set?

Reported by: NeilS Assigned to: jcm
Priority: major Milestone: 0.1 with tests
Component: plugin Version: 0.1
Keywords: Cc:

Description

Hello there.

I'm running Rails 1.1.6, Ruby 1.8.4 on Windows XP. I checked out the plugin from SVN today into vendor/plugins, so it should be the latest version.

However, I'm running SQLite3 - which is where I guess the problem arises (unless this problem exists for everyone - which I hope it doesn't!)

I have a model called "Category" which acts_as_nested_set. If I run script/console (in a new database) and set some things up like this:

main = Category.new(:name => 'Main')
main.save
sub = Category.new(:name => 'Sub Category')
sub.save

sub.move_to_child_of main

sub.save
main.save

(I'm not really sure which of those saves are necessary, but I'm playing it safe!)

I get some odd results:

main.id                # returns 1 - ok
sub.id                 # returns 2 - ok

main.children          # returns sub object - ok
sub.parent             # returns main object - ok

main.children_count    # returns 0 - ???
main.full_set          # returns just main object - ???
main.all_children      # returns [] - ???

The problem appears to be in the setting of rgt. I assume from the readme that the situation right now should be:

MAIN - id 1 - lft 1 - rgt 4 ... SUB - id 2 - lft 2 - rgt 3

But in actual fact:

main.lft   # 1 - ok
sub.lft    # 2 - ok
sub.rgt    # 3 - ok

main.rgt   # 2 - ???

Is this SQLite3 specific? Any plans to resolve this, or pointers to how I might be able to solve this?

Many thanks in advance for any advice you may be able to give.

Regards, NeilS.

Change History

10/10/06 13:39:44 changed by jcm

  • status changed from new to assigned.
  • component changed from component1 to plugin.
  • priority changed from minor to major.
  • version set to 0.1.
  • milestone set to 0.1 with tests.
  • owner changed from somebody to jcm.

What you described is typically an error !

Could you please show what are lft and rgt values after

main = Category.new(:name => 'Main')
main.save

and

sub = Category.new(:name => 'Sub Category')
sub.save

I suppose you start from an empty table, isn't it ? Do you use scope ? (it's known to be buggy atm).

11/15/06 16:26:23 changed by Krishna

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

Hi NeilS,

This problem is not unique to SQLite, and is caused by stale objects. The move_to_child_of method updated main in the database, but not the object. When you called main.save, the (stale) object overwrote the (correct) information in the database. I have added target.reload to the end of move_to, and now the following tests pass:

  def test_ticket_17
    main = Category.new
    main.save
    sub = Category.new
    sub.save
    sub.move_to_child_of main
    sub.save
    main.save
    
    assert_equal(1, main.children_count)
    assert_equal([main, sub], main.full_set)
    assert_equal([sub], main.all_children)
    
    assert_equal(1, main.lft)
    assert_equal(2, sub.lft)
    assert_equal(3, sub.rgt)
    assert_equal(4, main.rgt)
  end

I will commit these changes to rubyforge in the next day or two (revision 18).