[英]Querying in rails - Double quoted string inside query
I need query like this 我需要这样的查询
scope :find_email_usage, -> (id) {
where("EXISTS ((with filter_table as (select id, jsonb_array_elements(jsonb_array_elements(filters) -> 'criteria') filter_json) select 1 from filter_table where filter_json @> '{\"mail_id\": ?}'))", id.to_s, id.to_s)
}
But the structure has to be like '{"a":1, "b":"2"}'::jsonb @> '{"b":2}'::jsonb
但是结构必须类似于
'{"a":1, "b":"2"}'::jsonb @> '{"b":2}'::jsonb
How do I get the double quote in raw query using the substitution using ? 如何使用替换使用原始查询中的双引号? in rails.
在铁轨上。
where("EXISTS ((with filter_table as (select id, jsonb_array_elements(jsonb_array_elements(filters) -> 'criteria') filter_json) select 1 from filter_table where filter_json @> '{\"mail_id\": "#{id.to_s}"))", id.to_s)
But any other ways to get the double quoted string inside raw query? 但是还有其他方法可以在原始查询中获取双引号字符串吗?
最简单的方法是构建一个Ruby哈希,将其转换为JSON,然后将该字符串提供给查询:
where('EXISTS ((... where filter_json @> ?::jsonb))', { 'mail_id' => id.to_s }.to_json)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.