如何设置ruby-on-rails中模型的依赖关系为dependent?

2026-04-28 02:141阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计968个文字,预计阅读时间需要4分钟。

如何设置ruby-on-rails中模型的依赖关系为dependent?

在 Rails 3.1.0 版本中,我遇到了几个关联模型的问题。其中一个模型使用 `has_many` 关联,具体是 `blocks`。在尝试删除这些 `blocks` 时,遇到了继承错误。错误信息显示为:

blocks dependent: :destroy 错误地销毁 buckets 记录

这通常意味着在删除 `blocks` 时,试图同时删除它们所属的 `buckets` 记录,而 `buckets` 记录有其他依赖关系,导致错误。

解决这个问题的方法是确保在删除 `blocks` 时,不要同时删除它们关联的 `buckets`。可以在删除 `blocks` 的代码中添加逻辑来避免这种情况。以下是修改后的代码示例:

ruby假设你有一个 BlocksController,你想在这里处理删除 blocksclass BlocksController

# 确保在删除 block 之前,不删除它关联的 bucket @block.bucket.destroy

# 现在可以安全地删除 block @block.destroy

respond_to do |format| format. { redirect_to blocks_url, notice: 'Block was successfully destroyed.' } format.json { head :no_content } end endend

在这个例子中,我们首先找到要删除的 `block`,然后单独删除与之关联的 `bucket`,最后再删除 `block` 本身。这样可以避免在删除 `block` 时意外删除 `bucket`。

Rails 3.1.0

我有几个关联模型.我的一个模型使用has_many:blocks继续错误地消除:blocks,dependent => :使用以下错误销毁:

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和: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' ... 我仔细检查了日志与成功之后想出了:依赖=> :从另一个模型中摧毁

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

这条线看起来应该在那里,但是在仔细检查时,ORDER BY子句不属于.

在我的Bucket模型中,我有一个rake任务的以下内容.

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

这导致了错误.没有什么比花几个小时来犯一个愚蠢的错误了.

如何设置ruby-on-rails中模型的依赖关系为dependent?

本文共计968个文字,预计阅读时间需要4分钟。

如何设置ruby-on-rails中模型的依赖关系为dependent?

在 Rails 3.1.0 版本中,我遇到了几个关联模型的问题。其中一个模型使用 `has_many` 关联,具体是 `blocks`。在尝试删除这些 `blocks` 时,遇到了继承错误。错误信息显示为:

blocks dependent: :destroy 错误地销毁 buckets 记录

这通常意味着在删除 `blocks` 时,试图同时删除它们所属的 `buckets` 记录,而 `buckets` 记录有其他依赖关系,导致错误。

解决这个问题的方法是确保在删除 `blocks` 时,不要同时删除它们关联的 `buckets`。可以在删除 `blocks` 的代码中添加逻辑来避免这种情况。以下是修改后的代码示例:

ruby假设你有一个 BlocksController,你想在这里处理删除 blocksclass BlocksController

# 确保在删除 block 之前,不删除它关联的 bucket @block.bucket.destroy

# 现在可以安全地删除 block @block.destroy

respond_to do |format| format. { redirect_to blocks_url, notice: 'Block was successfully destroyed.' } format.json { head :no_content } end endend

在这个例子中,我们首先找到要删除的 `block`,然后单独删除与之关联的 `bucket`,最后再删除 `block` 本身。这样可以避免在删除 `block` 时意外删除 `bucket`。

Rails 3.1.0

我有几个关联模型.我的一个模型使用has_many:blocks继续错误地消除:blocks,dependent => :使用以下错误销毁:

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和: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' ... 我仔细检查了日志与成功之后想出了:依赖=> :从另一个模型中摧毁

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

这条线看起来应该在那里,但是在仔细检查时,ORDER BY子句不属于.

在我的Bucket模型中,我有一个rake任务的以下内容.

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

这导致了错误.没有什么比花几个小时来犯一个愚蠢的错误了.

如何设置ruby-on-rails中模型的依赖关系为dependent?