ActiveRecord joins syntax
I need to express the following join condition using ActiveRecord:
SELECT ...
FROM U
LEFT OUTER JOIN F ON U.key = F.foreign_key
AND F.key = ?
WHERE ...
where ? is substituted at run time.
The following causes a SQL-syntax error:
joins("LEFT OUTER JOIN F on U.key = F.foreign_key AND F.key=?", key)
I can't seem to determine if Activ开发者_运维技巧eRecord supports this 'dynamic substitution' (whatever this is called).
Adding the restriction (where("F.key=?", key)) in the WHERE clause will collapse the OUTER JOIN to a JOIN.
This syntax worked:
joins("LEFT OUTER JOIN F on U.key = F.foreign_key AND F.key=#{key}")
精彩评论