I added recently a jsonb field to one of my tables
add_column :users, :preferences, :jsonb, null: false, default: '{}'
and I am storing values like this :
user = User.first
user.preferences = { job: "programmer" }
now I would like to push another key-value pair to my jsonb field, I tried this in the console :
user = User.first
user.preferences.merge(twitter_account: "something")
user.save
and I get something like
....
(0.3ms) COMMIT
=> true
which normally means it's saved ! but when I check the value of preferences
I don't see any added value, even when I exit the console and re-run rails console again, I can see any change
Is that the way to do it or I am wrong ?
When you use merge it return hash with added values but to add values to hash you need to use merge!
I think that right way will be
user.preferences = user.preferences.merge(twitter_account: "something")