运维开发网

ruby-on-rails – ActiveRecord表别名

运维开发网 https://www.qedev.com 2020-08-11 15:21 出处:网络 作者:运维开发网整理
有谁知道是否有可能为ActiveRecord表连接设置别名? 就像是: User.find(:all, :alias => "Users as u", :joins => "Friends as f", :select => "u.id,f.name") 有任何想法吗? 是的,但如果覆盖连接,则需要包含“ON”语句和join语句. User.find(:all, :joins => " as u
有谁知道是否有可能为ActiveRecord表连接设置别名?

就像是:

User.find(:all, :alias => "Users as u", :joins => "Friends as f", :select => "u.id,f.name")

有任何想法吗?

是的,但如果覆盖连接,则需要包含“ON”语句和join语句.

User.find(:all, :joins => " as u INNER JOIN Friends as f ON f.user_id = u.id", :select => "u.id,f.name")

或者在Rails 3中

User.joins("as u INNER JOIN Friends as f on f.user_id = u.id")
    .select("u.id, f.name")
    .all

扫码领视频副本.gif

0

精彩评论

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

关注公众号