1

So i have a this column in a table t.text "foo", default: [], array: true

And i have this array list in a helper file,

module HelperData

  Helper_array = [
    "data_1",
    "data_2",
    "data_3",
    "data_4",
    "data_5"
  ]
end

Inside the FooName.rb model there's a line include HelperData.

So the query i want to pass is the include operator && in comparing PG arrays, based on this Postgres Guide.

I have tried the following queries:

FooName.where("foo" && HelperData::Helper_array). This query returns the error ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:. The syntax error is with the elements in HelperData::Helper_array. I have a slight hunch that it is because Foo is :text but the data in Helper_array is a string, though i can't be certain.

FooName.where("foo && HelperData::Helper_array"). This query returns the error ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:. Obviously this is just the wrong syntax then.

What is the proper query to find whether the foo array has any elements that overlap with the Helper_array, and return all FooName objects that has overlapping elements?

1 Answer 1

2

try:

FooName.where("foo && ARRAY[?]::text[]", HelperData::Helper_array)

refer this link https://www.postgresql.org/docs/current/static/functions-array.html

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

3 Comments

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: text[] && character varying[] Throws this error.
em, sorry for that, find out that the column is text type, not string type. so try FooName.where("foo && ARRAY[?]::text[]", HelperData::Helper_array). i have test it in my rails console, it's working.
Okay thanks! I'll test it out and check your answer as correct if it works! :)

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.