开发者

ActiveRecord joins syntax

开发者 https://www.devze.com 2023-02-08 03:46 出处:网络
I need to express the following join condition using ActiveRecord: SELECT ... FROM U LEFT OUTER JOIN F ON U.key = F.foreign_key

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}")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号