gpt4 book ai didi

ruby-on-rails - :dependent => :destroy not working for a model, rails 3.1.0

转载 作者:行者123 更新时间:2023-12-01 10:07:34 25 4
gpt4 key购买 nike

rails 3.1.0

我有几个关联模型。我的一个模型在使用 has_many :blocks, :dependent => :destroy 进行销毁时不断出错,并出现以下错误:

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"
...

我的 block 模型:

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"
...

当我删除一个模式时,它会毫无问题地删除关联的 block 或桶。我只是无法在没有错误的情况下删除 Bucket(和关联的 block )。我试过 :delete_all 和 :nullify 时出现同样的错误。

有什么想法吗?

日志

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'
...

最佳答案

我在仔细检查日志与成功的 :dependent => :destroy 来自另一个模型后发现了这一点。

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

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

在我的 Bucket 模型中,我有以下用于抽取任务的内容。

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

这是导致错误的原因。没有什么比在一个愚蠢的错误上花费几个小时更好的了。

关于ruby-on-rails - :dependent => :destroy not working for a model, rails 3.1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8981723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com