【发布时间】:2020-07-01 12:14:27
【问题描述】:
我正在使用 Rails nested attributes 和 allow_destroy: true。如果我这样称呼:
deck.update(deck_items_attributes: { id: 1000, _destroy: true })
并且 ID 为 1000 的 deck_item 不存在 Rails 引发异常 ActiveRecord::RecordNotFound。
有没有办法告诉 Rails 不要抛出异常而忽略该记录?
【问题讨论】:
-
我看到的唯一方法是重写您的
Deck模型的方法#update -
@AlexGolubenko 我不明白你为什么删除你的答案。你在那里 75%。所缺少的只是检查属性是否存在。
-
@max 我已经在我的本地检查过,
reject_if没有在update上运行,也许我错过了一些东西,但是..
标签: ruby-on-rails ruby nested-attributes accepts-nested-attributes