目前我将rails 2应用程序升级到rails 3.2.13,使用acts_as_tree和acts_as_list。之前,升级运行良好。因为,我对使用依赖destroy的acts_as_tree和acts_as_list有问题。
我必须建模:
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
鉴于我有一个用户:
<User id:1>
2个类别和其中一个类别是另一个类别的孩子:
<Category id:1 parent_id:nil user_id:1>
<Category id:2 parent_id:1 user_id:1>
当我试图销毁用户时,dependent: :destroy
会尝试销毁类别。它摧毁了第一类。由于该类别有一个孩子(id 2),act_as_tree也会将其销毁。但是似乎acts_as_list试图重新加载第二类的位置。当然,它不再存在,所以它提出了:
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'
任何人都有想法解决它?
PS:Rails 3.2.13,Ruby 1.9.3,acts_as_tree 1.4.0和acts_as_list 0.2.0
答案 0 :(得分:0)
我通过将acts_as_list降级为0.1.8
解决了这个问题问题是由于此拉取请求:https://github.com/swanandp/acts_as_list/commit/f62c43265f0e47fe7666a93849abad5aa7c5c6d3