2012-01-24 22 views
3

Rails 3.1.0:dependent =>:破坏不适用于模型,Rails 3.1.0

我有几个模型与关联。我的一个车型不断用的has_many摧毁示数出来:块:依赖=>:

NoMethodError in BucketsController#destroy 

undefined method `delete_all' for #<Array:0x007ffd0cea9bb8> 

我斗型号:

class Bucket < ActiveRecord::Base 
    require 'erb' 
    include ERB::Util 
    require 'rdiscount' 

    has_paper_trail :skip => [:lock_version] 

    has_many :blocks, :dependent => :destroy #tried delete_all, nullify, same error 
    belongs_to :folder 
    belongs_to :pattern 
    belongs_to :user, :class_name => "User", :foreign_key => "updated_by" 
    ... 

我的块体模型:

与下面的错误破坏
class Block < ActiveRecord::Base 
    require 'erb' 
    include ERB::Util 
    require 'rdiscount' 

    has_paper_trail :skip => [:lock_version] 

    belongs_to :list 
    belongs_to :pattern 
    belongs_to :bucket 
    belongs_to :user, :class_name => "User", :foreign_key => "updated_by" 
    acts_as_list :scope => :bucket 
    ... 

我的模式模型(正常工作)

class Pattern < ActiveRecord::Base 
    has_paper_trail :skip => [:lock_version] 

    has_many :blocks, :dependent => :destroy 
    has_many :buckets, :dependent => :destroy 
    belongs_to :user, :class_name => "User", :foreign_key => "updated_by" 
    ... 

当我删除一个模式时,它删除没有问题的相关块或存储桶。我无法删除一个Bucket(和相关的块)而没有错误。我试过:delete_all和:用相同的错误取消。

任何想法?

登录

Started DELETE "/buckets/4" for 127.0.0.1 at 2012-01-23 20:16:25 -0700 
Processing by BucketsController#destroy as HTML 
Parameters: {"authenticity_token"=>"+Bv+RYtusfOYfRkgYwC2Ptaj9brW1412NuVoxe5rD/4=", "id"=>"4"} 
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 
Bucket Load (0.5ms) SELECT `buckets`.* FROM `buckets` WHERE `buckets`.`id` = 4 LIMIT 1 
Role Load (0.4ms) SELECT `roles`.* FROM `roles` INNER JOIN `roles_users` ON `roles`.`id` = `roles_users`.`role_id` WHERE `roles_users`.`user_id` = 1 AND `roles`.`title` = 'SuperAdmin' LIMIT 1 
CACHE (0.0ms) SELECT `buckets`.* FROM `buckets` WHERE `buckets`.`id` = LIMIT 1 
(0.1ms) BEGIN 
Block Load (0.5ms) SELECT `blocks`.* FROM `blocks` WHERE `blocks`.`bucket_id` = 4 ORDER BY position, id 
(0.4ms) ROLLBACK 
Completed 500 Internal Server Error in 150ms 

NoMethodError (undefined method `delete_all' for #<Array:0x007ffd0cea9bb8>): 
app/controllers/buckets_controller.rb:67:in `destroy' 

跟踪

activerecord (3.1.0) lib/active_record/associations/builder/has_many.rb:49:in `block in define_delete_all_dependency_method' 
activesupport (3.1.0) lib/active_support/callbacks.rb:395:in `_run_destroy_callbacks' 
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks' 
activerecord (3.1.0) lib/active_record/callbacks.rb:254:in `destroy' 
activerecord (3.1.0) lib/active_record/transactions.rb:236:in `block in destroy' 
activerecord (3.1.0) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status' 
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction' 
activerecord (3.1.0) lib/active_record/transactions.rb:208:in `transaction' 
... 
+0

我也尝试在block模型中考虑可能是问题的acts_as_list。没有运气。 –

+0

也升级到3.1.3,以确保它不是一个错误。没有运气。 –

+0

我终于明白了。我的8小时保持时间结束后将发布结果。 –

回答

7

我想通了VS成功日志的仔细检查之后:依赖=>:从另一个模型破坏。

Block Load (0.5ms) SELECT `blocks`.* FROM `blocks` WHERE `blocks`.`bucket_id` = 4 
\ORDER BY position, id 

此行看起来应该在那里,但仔细检查ORDER BY子句不属于。

在我的桶模型中,我对耙子任务有以下几点。

def blocks 
    Block.where(:bucket_id => self.id).order(:position, :id).all 
end 

这是导致错误。没有什么比一个愚蠢的错误花上几个小时。