Search code examples
ruby-on-railsruby-on-rails-4rails-activerecordpg

How does ActiveRecord convert a hash to JSONB?


I have a hash that contains only string keys and simple datatypes that are compatible with JSON (numeric values, arrays, string, and objects containing the same).

I'd like to know what ActiveRecord (or any other layers involved) does to translate that ruby hash into the SQL statement that stores it in a JSONB column.

Using rails 4.2.7.1, and pg 0.20.0

Does ActiveRecord just call #to_json on the hash, and the resulting String is put in the SQL query, and that's it?


Solution

  • They called ActiveSupport::JSON.encode method, as you can see from lines.

    But later it was changed in this commit, which caused problems mentioned in the bug ticket, and the notes.