madeintandem / jsonb_accessor

Adds typed jsonb backed fields to your ActiveRecord models.
MIT License
1.11k stars 93 forks source link

Assigning undefined jsonb values overwrites non-jsonb values in v1.3.3 #148

Closed yurikoval closed 2 years ago

yurikoval commented 2 years ago

v1.3.3 seems to have broken attribute assignment for values that are not defined in jsonb. If you happen to have a key that has the same name as an attribute (non-jsonb) in ActiveRecord, it ends up overwriting it. Expected behaviour would be to store the data, but not overwrite non-jsonb attributes.

create_table "products" do |t|
  t.jsonb "data"
end

class Product < ActiveRecord::Base
  jsonb_accessor :data, title: :string
end
product = Product.new
product.created_at # nil
product.data = {title: "hello world", created_at: 1.year.ago}
product.title # "hello world"
product.created_at # "2021-08-19T15:40:42+08:00" (this should be nil)
haffla commented 2 years ago

Hello @yurikoval,

thanks for reporting this. I believe this bug had existed in v1.3.3 and before, too.

I've released v1.3.5 that fixes the issue.