How to escape the ? (question mark) operator to query Postgresql JSONB type in Rails -


i'm working rails 4.2 , postgres 9.4 try out new jsonb data type. 1 of jsonb columns in database holds array, , want able query records array contains value. figured out how using new jsonb "question mark" ("contains") operator, documented here: http://www.postgresql.org/docs/9.4/static/functions-json.html

so in raw sql can work in example:

select * people roles ? '32486d83-4a38-42ba-afdb-f77ca40ea1fc'; 

but can't see way query within rails via activerecord. i've tried doing raw query using "where" method, follows:

person.where("roles ? ?", "32486d83-4a38-42ba-afdb-f77ca40ea1fc") 

but since question mark special character used replace parameters, error:

activerecord::preparedstatementinvalid: wrong number of bind variables (1 2) in: roles ? ?

i guess need way escape "?" character since want pass through literally. i've tried \? , ?? no luck. appreciated!

you should call below :

person.where("roles ? :name", name: "32486d83-4a38-42ba-afdb-f77ca40ea1fc") 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -