0
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?

1 Answer 1

1

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 [...]

Sign up to request clarification or add additional context in comments.

1 Comment

You're usually better off using the array[...] syntax (where('origin @> array[?]', '*')), fewer quoting issues that way and AR will even expand an array into the placeholder properly without any extra chicanery. With one element you could also say ? = any(origin).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.