Search code examples
ruby-on-railsmodelstorejsonb

JSONB: store_model gem. Modify content on posting data


I'm using StoreModel gem to wrap my JSON-backed DB column setting with an ActiveModel-like classes. This is my model simplified model

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

For the form I have the following

<%= form_for @entity do |f| %>
    <%= fields_for :settings, @entity.settings do |ff| %>
        <%= ff.check_box :setting1 %>
        <%= ff.check_box :setting2 %>
    <% end %
<% end %>

For an existing record, this would overwrite all values in the settings attribute of my Entity model, hence it would set setting3 no null (which is not passed by the form / params)! How can I submit values to keep existing values and just modify ones I submit.


Solution

  • It was discussed in the GitHub issue earlier, TLDR it's not possible out–of–the–box, but there is a workaround.