![](/img/trans.png)
[英]What does #{$$} inside a double quoted string in Ruby on Rails ?
[英]Querying in rails - Double quoted string inside query
我需要這樣的查詢
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)
}
但是結構必須類似於'{"a":1, "b":"2"}'::jsonb @> '{"b":2}'::jsonb
如何使用替換使用原始查詢中的雙引號? 在鐵軌上。
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)
但是還有其他方法可以在原始查詢中獲取雙引號字符串嗎?
最簡單的方法是構建一個Ruby哈希,將其轉換為JSON,然后將該字符串提供給查詢:
where('EXISTS ((... where filter_json @> ?::jsonb))', { 'mail_id' => id.to_s }.to_json)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.