Я использую postgresql.
У меня есть таблица с именем custom_field_answers. Данные выглядят так:
Id | product_id | value | number_value |
4 | 2 | | 117 |
3 | 1 | | 107 |
2 | 1 | bangle | |
1 | 2 | necklace | |
Я хочу найти все продукты, у которых text_value равно 'bangle' и number_value меньше 50.
SELECT p.*
FROM "products" AS p
INNER JOIN "custom_field_answers" AS a1 ON p."id" = a1."product_id"
INNER JOIN "custom_field_answers" AS a2 ON p."id" = a1."product_id"
WHERE a1."value" = 'bangle' AND a2."number_value" < 50
Я пытался создать этот sql со следующим кодом.
conditions = <conditions from arel>
relation = self.scoped
conditions.each do |condition|
relation = relation.merge(where(condition))
end
joins(:custom_field_answers).merge(relation)
relation.to_a
Это производит следующий sql
SELECT "products".* FROM "products" INNER JOIN "custom_field_answers"
ON "custom_field_answers"."product_id" = "products"."id"
WHERE ("custom_field_answers"."value" ILIKE 'bangle')
AND ("custom_field_answers"."number_value" < 50)
Как вы можете видеть, этот sql не похож на нужный sql (упомянутый вверху).
Я попытался немного переместить код соединения
relation = self.scoped
conditions.each do |condition|
relation = relation.merge(where(condition).joins(:custom_field_answers))
end
relation.to_a
Все еще не повезло.
Любой знает, как форсировать новое соединение для каждого отношения. Я использую Rails 3.1.1.