我正在尝试更新嵌套记录,但由于某种原因它不起作用并且我的更新操作被忽略了。如果我在控制台中运行以下代码,它返回 true,但实际上没有为 field_values_attributes 更新任何内容,只有 steps_attributes 按预期工作 = 状态更新为 6。如果我删除“id”=>“35”,则创建新的 field_value很好,所以UPDATE有问题。传递 id 和 _destroy 参数时,DESTROY 操作也有效。
# CREATE - WORKING
myrecord.update("status"=>6, "field_values_attributes"=>[{"value"=>"new_value"}])
# UPDATE - NOT WORKING
myrecord.update("status"=>6, "field_values_attributes"=>[{"id"=>"35", "value"=>"new_value"}])
# DESTROY- WORKING
myrecord.update("status"=>6, "field_values_attributes"=>[{"id"=>"35", "_destroy"=>"1"}])
字段值模型
# FieldValue.rb
class FieldValue < ApplicationRecord
attr_accessor :value
belongs_to :field, class_name: 'FieldInput', foreign_key: 'field_component_id'
before_validation :set_value
private
def set_value
# when run in debug mode, the following code is not executed / ignored for UPDATE action, working fine for CREATE action
case field.field_type.key
when 'text'
self.text_value = value
when 'number'
self.number_value = value
when 'date'
else
self.string_value = value
end
end
执行步骤.rb
class ExecutionStep < ApplicationRecord
has_many :field_values, class_name: 'FieldValue', as: :valueable, dependent: :destroy
enum status: { not_started: 0, in_progress: 1, paused: 3, retry: 4, completed: 5, failed: 6, canceled: 7 }
accepts_nested_attributes_for :field_values, allow_destroy: true
end
过去还有其他人遇到过同样的问题吗?谢谢,米罗