Model Listing has a json column named vendors, which defaults to {}:
add_column :listings, :vendors, :json, null: false, default: {
hd: {}
...
}
end
Inside the Listing class, I am trying to merge! in a hash to hd: {..} like so:
def self.append_vendor_attrs(sku)
listing = find_by(sku: sku) || Listing.new(sku: sku)
listing.vendors[:hd].merge!({ foo: 'bar' })
listing.vendors_will_change!
listing.save! # throws no exceptions in the log
end
but vendors[:hd] remains empty. if I try to replicate this manually with:
l = Listing.create...
l.vendors[:hd].merge!({ foo:'bar' })
my hash persists.
listing.vendors_will_change!above the line where you modify the attribute in place? My hunch is that the code you have in your method tells AR that the value of the attribute was the new value, so that it doesn't see the object as dirty when you callsave!.will_changeafter the data was already appended, triggering AR to "listen" to any changes to that row but nothing was changed. Go Brooklyn! Turn this into an answer so I can accept?