私はしばらくこれを試してきましたが、Activerecord ではうまくいかないようです。
asset_id
とのペアの配列が与えられた場合、両方の属性とが一致asset_type
する場合にのみ、両方の属性を持つクラスをクエリします。asset_id
asset_type
与えられた配列
[[4,"Logo"],[1,"Image"]]
SQLを生成したい
SELECT "asset_attachments".* FROM "asset_attachments" WHERE ((asset_id,asset_type) IN ((4,'Logo'),(1,'Image')))
次のように where を使用して文字列を手動で入力することで、これを行うことができます。
AssetAttachment.where("(asset_id,asset_type) IN ((4,'Logo'),(1,'Image'))")
しかし、任意の長さとアセット タイプ/ID の配列で使用しようとしています。
これまで私は試しました
AssetAttachment.where([:asset_id, :asset_type] => [[4,"Logo"],[1,"Image"]])
NoMethodError: undefined method `to_sym' for [:asset_id, :asset_type]:Array
と
AssetAttachment.where("(asset_id,asset_type)" => [[4,"Logo"],[1,"Image"]])
ActiveRecord::StatementInvalid: PG::Error: ERROR: column asset_attachments.(asset_id,asset_type) does not exist
と
AssetAttachment.where("(asset_id,asset_type) IN (?,?)",[[4,"Logo"],[1,"Image"]])
ActiveRecord::PreparedStatementInvalid: wrong number of bind variables (1 for 2) in: (asset_id,asset_type) IN (?,?)
誰もこれを行う方法を知っていますか? 前もって感謝します