If you have an attribute that needs to be saved to the database as an
object, and retrieved as the same object, Ruby on Rails offer a
class method inside an ActiveRecord model which we can specify what
kind of data are stored in a column and Rails would automatically take
care of converting/parsing the actual values.
Here is a great post showing how to make use of the
Ruby on Rails:
To set a default value, Array, and Hash type works as normal, but the JSON is a little bit different. It took me a while to figure out the right way to set the default for JSON type. Here is the answer:
1 2 3 4 5 6 7 8 9
For a deep understanding, let’s take a look under the hood.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
A serialized attribute will always be updated during save, even if it was not changed.