【发布时间】: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