开发者

ActiveRecord Table Aliases

开发者 https://www.devze.com 2023-01-06 18:18 出处:网络
Does anyone know if it\'s somehow possible to setup an alias for an ActiveRecord 开发者_开发知识库table join?

Does anyone know if it's somehow possible to setup an alias for an ActiveRecord 开发者_开发知识库table join?

Something like:

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

Any ideas?


Yes, but you need to include the 'ON' statement and the join statement if you overwrite the join.

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

or in Rails 3+

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

精彩评论

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

关注公众号