示例 样板房,专栏:动物。 动物 jsonb 内部是这样的:
dogs: {
dog1: { attributes...},
dog2: { attributes...},
...
}
问题
如何仅通过key("dog1")找到dog1
最佳答案
Active Record 本身不支持此功能。您必须编写一些自定义的 where 子句。例如在 Postgres 中,请参阅此处的文档:https://www.postgresql.org/docs/9.4/functions-json.html .
此处列出了一些示例:https://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails ,在“查询您的 jsonb 列”部分下!
# preferences->twitter OR preferences->github
User.where('preferences ?| array[:keys]', keys: ['twitter', 'github'])
关于sql - 如何在 Rails 中查找 jsonb 中的哈希键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60961264/