add_column :ssr_service_markets, :origin, :string, array: true, default: []
and when i want to
SSRService::Market.where(origin: "*", destination: "*").first
I have got
PG::InvalidTextRepresentation: ERROR: malformed array literal: "*" LINE 1: ...service_markets" WHERE "ssr_service_markets"."origin" = '*' DETAIL: Array value must start with "{" or dimension information
How to fix it?
In Postgres, to check if an array contains given element, you can use @>
array operator.
Read more: Array Operators
:origin
is an array field in your ssr_service_markets
table, which means that it can contain multiple values.
In your example, assuming that destination
is an array field as well, you could try to do the searching this way:
Model.where(["origin @> ? AND destination @> ?", '{*}', '{*}')"])
Remember about using curly braces when working with array values .
To write an array value as a literal constant, enclose the element values within curly braces [...]
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.