ActiveRecord Table Aliases

Does anyone know if it is possible to somehow configure an alias to join an ActiveRecord table?

Sort of:

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

Any ideas?

+3
source share
1 answer

Yes, but you need to enable the 'ON' statement and the join operator if you rewrite the connection.

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
+7
source

Source: https://habr.com/ru/post/1754332/


All Articles