【问题标题】:ROR: How to handle with "Failed to destroy the record" execption?ROR:如何处理“无法销毁记录”异常?
【发布时间】:2017-04-27 09:30:42
【问题描述】:

当我破坏一个带有答案的语句时,它会出现一个错误(并在 flash 中显示)。

但是当我尝试使用此语句删除测试时,它会出现“无法销毁记录”。

class Test < ActiveRecord::Base
  has_many :statements, dependent: :destroy
  has_many :answers, through: :statements

class Statement < ActiveRecord::Base
  belongs_to :test
  has_many :answers, dependent: :restrict_with_error

我需要:

  • 当有语句而没有语句时测试被销毁 有答案。
  • 当有 Statement 且任何 Statement 有 Answers 时,测试不会被销毁。

像这样的:

class Test < ActiveRecord::Base
      has_many :statements, dependent: :destroy
      has_many :answers, through: :statements, dependent: :restrict_with_error

我的做法:

test_controller.rb

def destroy
    if @test.destroy
      redirect_to tests_path, notice: 'Good'
    else
      redirect_to tests_path, error: 'Bad'
    end
end

我的方法引发错误:

>> @test.destroy
!! #<ActiveRecord::RecordNotDestroyed: Failed to destroy the record>

【问题讨论】:

  • 每当“测试”记录被销毁时,您是否只想自动销毁关联的语句?但不破坏陈述的答案?
  • @Jay-ArPolidario 编辑了我的问题

标签: ruby-on-rails activerecord associations


【解决方案1】:

现在你的问题更清楚了:

您的代码应该已经可以正常工作了,因为我刚刚使用相同的模型对其进行了测试。但是,我的猜测是,在销毁包含有答案的语句的测试时,您没有收到 Flash 消息?那么,也许您没有正确设置 flash 消息错误,请参见下文。

# models/test.rb
class Test < ActiveRecord::Base
  has_many :statements, dependent: :destroy
  has_many :answers, through: :statements

# models/statement.rb
class Statement < ActiveRecord::Base
  belongs_to :test
  has_many :answers, dependent: :restrict_with_error

# controllers/tests_controller.rb
def destroy
  @test = Test.find(params[:id])

  puts @test.errors.full_messages
  # => nil

  if @test.destroy
    puts @test.errors.full_messages
    #=> nil

    redirect_to tests_path, notice: 'Test was successfully destroyed.'

  else
    puts @test.errors.full_messages
    #=> nil

    @test.statements.each do |statement|
      puts statement.errors.full_messages
      # => ["Cannot delete record because dependent answers exist"]

      statement.errors.full_messages do |full_message|
        @test.errors.add(:base, "statement(id: #{statement.id}): #{full_message}")
      end
    end

    puts @test.errors.full_messages
    # => ["statement(id: 1): Cannot delete record because dependent answers exist"]


    redirect_to tests_path, alert: @test.errors.full_messages
  end
end

测试

检查我的测试是否正确反映了我对您的问题的理解:

当有具有答案的语句时不应销毁测试:

irb(main):001:0 > test = Test.create!
irb(main):002:0 > statement = Statement.create!(test: test)
irb(main):003:0 > answer = Answer.create!(statement: statement)
irb(main):004:0 > test_that_has_statement_that_has_answer = test
irb(main):005:0 > test_that_has_statement_that_has_answer.destroy
=> false
irb(main):006:0 > puts test.persisted?
=> true
irb(main):007:0 > puts test_that_has_statement_that_has_answer.errors.full_messages
=> nil
irb(main):008:0 > puts test_that_has_statement_that_has_answer.statements.first.errors.full_messages
=> ["Cannot delete record because dependent answers exist"]

当有没有答案的语句时应该销毁测试:

irb(main):001:0 > test = Test.create!
irb(main):002:0 > statement = Statement.create!(test: test)
irb(main):003:0 > test_that_has_statement_that_has_no_answer = test
irb(main):004:0 > test_that_has_statement_that_has_no_answer.destroy
=> #<Test id: 1...>
irb(main):005:0 > puts test.persisted?
=> false
irb(main):006:0 > puts test_that_has_statement_that_has_no_answer.errors.full_messages
=> nil
irb(main):007:0 > puts test_that_has_statement_that_has_no_answer.statements.count
=> 0
# count becomes 0 which means the statement is automatically destroyed as well

【讨论】:

  • 在这种情况下,当Test有ANY语句时不能删除。我编辑了我的问题来解释这个想法。
  • 谢谢,详细解释,但现在问题出在 test.destroy 方法上。 &gt;&gt; @test.destroy!! #&lt;ActiveRecord::RecordNotDestroyed: Failed to destroy the record&gt;。在我上面的问题中添加了它。
  • @nuT707 似乎您的问题是由不同的原因引起的,因为我没有遇到任何 ActiveRecord::RecordNotDestroyed 错误。也许您有一个导致此错误的before_destroy 回调?或者可能检查导致错误的每个关联的 Statement 记录?尝试打开这个LINK 看看是否有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多