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
函数将数组转换为一组行。检查此答案:非常感谢!我最终抛弃了整个概念,非常感谢!我最终放弃了整个概念