I am having problems updating the rails JSON column in the model.
I have an OrderItem model with a column: options with a postgres json data type.
I have a form where I am only trying to update two attributes in this JSON column. However, when I run the update action from my controller, it resets the entire parameter column with only fields in my form, and not just updates two separate attributes. This is mistake? I'm on Rails 4.2.1 and from what I read it should work, and I can manually update individual attributes in the rails console.
In my controller:
def update @order_item.update(order_item_params) respond_with(:update) end private def order_item_params params.require(:order_item).permit(:product_id, :quantity, options:[:esp, :size]) end
My form:
<%= f.fields_for :options do |option| %> <%= option.label :size %> <%= option.select :size, options_for_select((5..13), item.options["size"]) %> <%= option.select :esp, options_for_select(["yes","no"], item.options["esp"]) %> <% end %>
When this form is submitted, it does not update individual attributes. It flushes the entire json column for only these two attributes
Form Data utf8:✓ _method:patch order_item[quantity]:2 order_item[options][size]:5 order_item[options][esp]:no
In the console, I can easily update attributes manually ...
o = OrderItem.last o.options["esp"] = "yes" o.save
source share