2017-01-31 9 views
1

私はこのテーブルを持っている:["Manufacturer,4", "Retail,6"]のようなので、Postgresのクエリは

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を取ります。私のどこクエリで

は、私は言葉「メーカー」との任意の受信者が含まれるパッケージを見つけるために探しています:

Package.where("sender_type = ? and recipients.include = ?", "school", regex).

私はどこにも近いこの

答えて

0

としていた場合だけ不思議検索している文字列が常に配列の最初の位置にある場合は、次のようにアクセスできます。

Package.where("sender_type = ? and recipients[0] LIKE = ?", "school", "Manufacturer").

テストされていないため、適応する必要があります。

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

それとも、行セットする配列をCONVERするunnest機能を使用することができます。

+0

大変感謝しています!私は全体のコンセプトを捨ててしまった。 – daveasdf