ActiveRecord表别名

时间:2010-07-13 00:39:38

标签: ruby-on-rails database activerecord join

有没有人知道是否可以为ActiveRecord表连接设置别名?

类似的东西:

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

有什么想法吗?

1 个答案:

答案 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