简体   繁体   English

Rails查询与Postgres数组列的比较

[英]Rails query comparing against Postgres Array Column

So i have a this column in a table t.text "foo", default: [], array: true 所以我在表t.text "foo", default: [], array: true有此列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 . FooName.rb模型内部,有一行include HelperData

So the query i want to pass is the include operator && in comparing PG arrays, based on this Postgres Guide . 因此,根据本《 Postgres指南》 ,我要传递的查询是在比较PG数组时包含运算符&&

I have tried the following queries: 我已经尝试了以下查询:

FooName.where("foo" && HelperData::Helper_array) . FooName.where("foo" && HelperData::Helper_array) This query returns the error ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: . 此查询返回错误ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: The syntax error is with the elements in HelperData::Helper_array . 语法错误与HelperData::Helper_array的元素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. 我有一点预感,这是因为Foo:text但是Helper_array的数据是string ,尽管我不确定。

FooName.where("foo && HelperData::Helper_array") . FooName.where("foo && HelperData::Helper_array") This query returns the error ActiveRecord::StatementInvalid: PG::UndefinedColumn: 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? 什么是正确的查询来查找是否foo阵列具有与重叠的任何元素Helper_array ,并将所有FooName已经重叠元素的对象?

try: 尝试:

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

refer this link https://www.postgresql.org/docs/current/static/functions-array.html 请参阅此链接https://www.postgresql.org/docs/current/static/functions-array.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM