Forum: Ruby on Rails acts_as_list, move_higher, odd indexing behaviour?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick Woolley (Guest)
on 2006-05-15 14:13
(Received via mailing list)
Hi,

[reposting to list, posting via google groups apparently didn't work]

I'm trying to use acts_as_list for the first time.

Can anyone confirm the odd behaviour reported here?

http://blog.nominet.org.uk/tech/Web/2006/03/06/Usi......

i.e. element access and move_higher require an odd indexing scheme to
work.

I'm personally finding that I can't seem to change the list element
positioning in a rails unit test using move_higher/move_lower, although
move_to_top works. (See code following below)

Cheers,

N

----

# model defs
class AccountTransaction < ActiveRecord::Base
   has_many :credits, :foreign_key => 'transaction_id', :order =>
'position ASC'
end

class Credit < ActiveRecord::Base
   belongs_to :account_transaction, :foreign_key => 'transaction_id'
   belongs_to :account
   acts_as_list :scope => :transaction_id
   belongs_to :credit_type
end

#unit test
class AccountTransactionTest < Test::Unit::TestCase
   fixtures :account_transactions, :credits

   def display_credits(transaction)
     transaction.credits.each_index do |x|
       credit = transaction.credits[x]
       puts " index #{x} id #{credit.id} position #{credit.position}"
     end
   end

   def test_credits
     assert_equal @account_transaction.credits[0].id, @first_credit.id
     assert_equal @account_transaction.credits[1].id, @second_credit.id

     display_credits @account_transaction

     @account_transaction.credits[1].move_higher # doesn't work
     @account_transaction.reload

     display_credits @account_transaction

     @account_transaction.credits[1].move_to_top # does work
     @account_transaction.reload

     display_credits @account_transaction

     assert_equal @account_transaction.credits[1].id, @first_credit.id
     assert_equal @account_transaction.credits[0].id, @second_credit.id

   end
end

# outputs:
# Loaded suite test/unit/account_transaction_test
# Started
#  index 0 id 1 position 1
#  index 1 id 2 position 8
#  index 0 id 1 position 1
#  index 1 id 2 position 8
#  index 0 id 2 position 1
#  index 1 id 1 position 2
# ..
# Finished in 0.188056 seconds.

# credits.yml
first_credit:
   id: 1
   transaction_id: 1
   position: 1
   account_id: 1
   amount: 11
   credit_type_id: 1

second_credit:
   id: 2
   transaction_id: 1
   position: 8
   account_id: 1
   amount: 18
   credit_type_id: 1

# account_transactions.yml
first_account_transaction:
   id:           1
   date:         23/01/2005
   description:  blah
   comments:     blah

another_account_transaction:
   id: 2
   date:         23/07/2005
   description:  blah
   comments:     blah
This topic is locked and can not be replied to.