我正在使用postgresql。如何多次加入同一個表
我有一個名爲custom_field_answers的表。數據看起來像這樣。
Id | product_id | value | number_value |
4 | 2 | | 117 |
3 | 1 | | 107 |
2 | 1 | bangle | |
1 | 2 | necklace | |
我想找到所有已TEXT_VALUE爲「手鐲」和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。