有没有人知道是否可以为ActiveRecord表连接设置别名?
类似的东西:
User.find(:all, :alias => "Users as u", :joins => "Friends as f", :select => "u.id,f.name")
有什么想法吗?
答案 0 :(得分:7)
是的,但如果覆盖连接,则需要包含'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