I’m trying to figure out how to add a new field with a default value into an existing JSON data field for an Active Record model.
The JSON field is called data
and has this current structure with these default values.
{“name”: “”, “number”: “”}
The current record in the database looks like this.
{“name”: “first”, “number”: “1111111111”}
I would like to add a new field to the JSON called ”address”
.
Now how would I create a rails migration to add this new field to the JSON structure without changing the current values for this record?
I know we can call add_column
or change_column
but I wasn’t sure how to add the new field without overwriting the exiting values for the record.
Is there a way to do that or do I have to save that data with its current values into a variable, then create a new migration that updates the model to add a new field for the JSON, and finally save the variable from earlier into the record to have its old values in addition to the new field?