Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord Table Aliases

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?

like image 810
matsko Avatar asked Dec 29 '22 10:12

matsko


1 Answers

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
like image 196
Geoff Lanotte Avatar answered Jan 13 '23 11:01

Geoff Lanotte