问题描述
我正在使用StoreModel gem将我的JSON支持的DB列setting
包装为类似ActiveModel的类。这是我的模型简化模型
class Entity < ApplicationRecord
attribute :settings,Setting.to_type # settings is a jsonb datatype in the database
end
class Setting
include StoreModel::Model
attribute :setting1,:boolean
attribute :setting2,:boolean
attribute :setting3,:boolean
end
对于表单,我具有以下内容
<%= form_for @entity do |f| %>
<%= fields_for :settings,@entity.settings do |ff| %>
<%= ff.check_Box :setting1 %>
<%= ff.check_Box :setting2 %>
<% end %
<% end %>
对于现有记录,这将覆盖我的实体模型的settings属性中的所有值,因此将set3设置为null(不通过表单/参数传递)!如何提交值以保留现有值,并只修改我提交的值。
解决方法
之前在GitHub issue中已进行了讨论,TLDR不可能即用即用,但是有workaround。