Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 涉及正则表达式数组的postgres查询_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails 涉及正则表达式数组的postgres查询

Ruby on rails 涉及正则表达式数组的postgres查询,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我有这张桌子: create_table "packages", force: :cascade do |t| t.string "sender_type" t.integer "sender_account_id" t.integer "sender_user_id" t.string "address" t.text "type" t.datetime "created_at", null: false t.datetime "u

我有这张桌子:

create_table "packages", force: :cascade do |t|
  t.string   "sender_type"
  t.integer  "sender_account_id"
  t.integer  "sender_user_id"
  t.string   "address"
  t.text     "type"
  t.datetime "created_at",        null: false
  t.datetime "updated_at",        null: false
  t.string   "recipients",                     array: true
end
收件人数组采用型号类型和id,用逗号分隔,如
[“制造商,4”,“零售,6”]

在我的where查询中,我希望找到包含任何带有“Manufacturer”字样的收件人的包:

Package.where(“发送者类型=?和接收者类型=?”,“学校”,正则表达式)。


只是想知道我是否与此非常接近如果您搜索的字符串始终位于数组的第一位,您可以这样访问它:

Package.where(“发件人类型=?和收件人[0]类似=?”、“学校”、“制造商”)。


未测试,因此您可能需要进行调整。

如果您搜索的字符串始终位于数组的第一位,则您可以这样访问它:

Package.where(“发件人类型=?和收件人[0]类似=?”、“学校”、“制造商”)。


未测试,因此您可能需要进行调整。

其中一种方法是将
最新版本
转换为
文本

Package.where("sender_type = ? and recipients::text LIKE ?", "school", regex)

或者可以使用
unest
函数将数组转换为一组行。检查此答案:

其中一种方法是将
收件人
转换为
文本

Package.where("sender_type = ? and recipients::text LIKE ?", "school", regex)

或者可以使用
unest
函数将数组转换为一组行。检查此答案:

非常感谢!我最终抛弃了整个概念,非常感谢!我最终放弃了整个概念