I've searched everywhere in SO and no lucks. Basically, i have this kind of array :
["a", "b", "c"]
I need to wrap this double quotes with single quotes to be like this :
['"a"', '"b"', '"c"']
The main reason for why i need to wrap this array element is because i need to query jsonb object in my database (PostgreSQL). The sample of query is like this :
data -> '#{af.column}' @> any(array#{@arr}::jsonb[])
To query jsonb object i need to put single quotes for each element.
UPDATE
Why i need to do this ? Its because, i need to combine multiple query into an array. Below is my example codes :
@conditions = args[:conditions] unless !args[:conditions].present?
@tables = ["assigned_contact"]
@query = self.joins({:assigned_contact => :call_campaign_answers})
.where(@conditions.join(" AND "))
.where("(data->>'age')::int between ? and ?", args[:min_age].to_i, args[:max_age].to_i)
.where("data -> 'gender' @> any(array[?]::jsonb[])", args[:gender].map(&:to_json))
.where("call_campaign_answers.answer IN (?)", args[:answers]).size
Where args[:conditions] are my query that i need to do wrape double quotes in single quotes.
If theres any other simple / method available, please let me know.
The problem I see is you're not returning a single JSON array, but multiple independent arrays which could be a syntax error. Use a singular JSON value:
.where("data -> 'gender' @> any(array[?]::jsonb[])", args[:gender].to_json)
Assuming
ary = %w[a b c]
#=> ["a", "b", "c"]
Then
new_ary = ary.map {|el| %Q["#{el}"] }
produces exactly your desired output:
new_ary == ['"a"', '"b"', '"c"']
#=> true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.