简体   繁体   English

ActiveRecord如何将哈希转换为JSONB?

[英]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). 我有一个哈希,其中仅包含与JSON兼容的字符串键和简单数据类型(数字值,数组,字符串和包含它们的对象)。

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. 我想知道ActiveRecord(或涉及的其他任何层)如何将红宝石哈希值转换为将其存储在JSONB列中的SQL语句。

Using rails 4.2.7.1 , and pg 0.20.0 使用rails 4.2.7.1pg 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? ActiveRecord是否仅在哈希上调用#to_json ,并将生成的String放入SQL查询中,仅此而已?

They called ActiveSupport::JSON.encode method, as you can see from lines . 他们调用了ActiveSupport::JSON.encode方法,正如您在lines中看到的那样。

But later it was changed in this commit , which caused problems mentioned in the bug ticket , and the notes . 但是后来在此提交中进行了更改,导致了错误注释中提到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM