Currently I upgrade rails 2 application to rails 3.2.13 wich use acts_as_tree and acts_as_list. Before, the upgrade it works well. Since, I have a problem with acts_as_tree and acts_as_list with dependent destroy.
I have to models:
class User < ActiveRecord::Base
has_many :categories, dependent: :destroy
end
class Category < ActiveRecord::Base
acts_as_tree
acts_as_list scope: [:user_id, :parent_id]
belongs_to :user
end
Given I have a user:
<User id:1>
And 2 categories and one of these categories is the children of the other:
<Category id:1 parent_id:nil user_id:1>
<Category id:2 parent_id:1 user_id:1>
When I try to destroy the user the dependent: :destroy
tries to destroy categories. It destroys the first category. As the category has a child (id 2) acts_as_tree destroy it too. But then it seems acts_as_list tries to reload positions of the second category. Naturally, it doesn't exist anymore so it raises:
Couldn't find Category with id=2
gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:343:in `find_one'
gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:314:in `find_with_ids'
gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:107:in `find'
gems/activerecord-3.2.13/lib/active_record/querying.rb:5:in `find'
...
gems/activerecord-3.2.13/lib/active_record/autosave_association.rb:226:in `reload'
gems/acts_as_list-0.2.0/lib/acts_as_list/active_record/acts/list.rb:409:in `reload_position'
gems/activesupport-3.2.13/lib/active_support/callbacks.rb:418:in `_run__108545607483159770__destroy__3935615418128680560__callbacks'
...
gems/activerecord-3.2.13/lib/active_record/callbacks.rb:254:in `destroy'
gems/activerecord-3.2.13/lib/active_record/transactions.rb:254:in `block in destroy'
gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
gems/activerecord-3.2.13/lib/active_record/transactions.rb:254:in `destroy'
Anyone has an idea to solve it?
PS: Rails 3.2.13, Ruby 1.9.3, acts_as_tree 1.4.0 and acts_as_list 0.2.0
I solved it by downgrading acts_as_list to 0.1.8
The issue is due to this pull-request: https://github.com/swanandp/acts_as_list/commit/f62c43265f0e47fe7666a93849abad5aa7c5c6d3