次のSQLがうまくいくようです
big_query = "
SELECT EXISTS (
SELECT 1
FROM buyables b1
JOIN buyables b2
ON b1.shop_week_id = b2.shop_week_id
AND b1.location_id = b2.location_id
WHERE
b1.parent_id != %1$d
AND b2.parent_id = %1$d
AND b1.type = 'Item'
AND b2.type = 'Item'
GROUP BY b1.parent_id
HAVING COUNT(*) = ( SELECT COUNT(*) FROM buyables WHERE parent_id = %1$d AND type = 'Item' )
)
"
ActiveRecordでは、select_value を使用してこの結果を取得できます。 :
class Basket < Buyable
def has_duplicate
!!connection.select_value( big_query % id )
end
end
ただし、パフォーマンスについてはよくわかりません