Detect postgreSQL json field changes

I use the json field to store some additional parameters in one of my models.

It works great, except for the fact that it does not detect the changes that I make when accessing data using square brackets:

2.1.1 :002 > p = Payments.last => {...} 2.1.1 :003 > p.params.keys => ["receipt_data"] 2.1.1 :004 > p.params['verification_data'] = 'test' => "test" 2.1.1 :005 > p.params.keys => ["receipt_data", "verification_data"] 2.1.1 :006 > p.params_changed? => false 2.1.1 :007 > p.save (0.2ms) BEGIN (0.2ms) COMMIT => true 2.1.1 :008 > Payment.last.params.keys Payment Load (0.5ms) SELECT "payments".* FROM "payments" ORDER BY "payments"."id" DESC LIMIT 1 => ["receipt_data"] 

How to make it save changes?

+6
source share
1 answer

to force, before any update. you can say:

 p = Payments.last p.params_will_change! p.params['verification_data'] = 'test' p.save 

Btw, ActiveRecord should handle dirty tracking automatically. so if you can click on an application on github that reproduces this problem, I can try to help.

+7
source

All Articles