I've been playing around Rails (4) + Postgres JSON fields a bit now, and I've noticed that if I do something like this
model.json_data = {
field1: "hello",
field2: "world"
}
model.save
it works fine. However if I do
model.update_column(:json_data, {
field1: "hello",
field2: "world"
} )
it doesn't work. It doesn't seem like update_column is storing the data as JSON, but just a string with line breaks and tabs included. The problem is, I want the json_data to be generated in an after_save callback, so I need to not re-trigger the after_save callback when updating the JSON field.
Any thoughts on what might be going on here, or how to get around it?
Nevermind, I found a solution.
model.update_column(:json_data, {
field1: "hello",
field2: "world"
}.to_json )
Seems obvious in hindsight.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With