If you’re using ActiveRecord 4.2+ you’re in luck. It carries support for
JSONb, and it’s cousin
JSON allow you to
store json natively as a column in your database tables. The power of these
is being able to then query directly against data stored in the data. For
instance the following query would be able to grab all email addresses where the
json column “preferences” has a key of contact_me set to true.
If you are using 9.4+ of Postgres you can take advantage of
supports indexing and faster key access of values. The 9.3 series only has
JSON; which is slower when doing queries because it has to perform
a table scan when performing a query and the entire json hash has to be read to
extract the value of a key from it.
Adding Column in a Rails Migration
Changing From a Text Column in a Rails Migration
- Use this if you are serializing json into a text column currently
- Make sure you always have a back-up of production before migrating!
Querying with ActiveRecord