0

I am using associations to join a number of models together and select them in a single query, however when two of the models share a column name, only the second one in the select statement is used. Is there any way of using a fully qualified name i.e. prefixed with the table name, so that the attributes hash can contain both column values?

Examples:

Class User < ActiveRecord::Base
  belongs_to :role
end

#This query will only allow me to see the name of the role
User.joins(:role).select('users.name', 'role.name')

#Using the raw connection removes the table name from the resultset also
User.connection.select("users.name, roles.name FROM users JOIN roles ON users.role_id = roles.id")

=> #<ActiveRecord::Result:0x00000000000000 @columns=["name", "name"], @rows=[["some_user", "admin"]]...
0

1 Answer 1

1

Try this:

User.joins(:role).select('users.name as user_name, role.name as role_name')

Sign up to request clarification or add additional context in comments.

1 Comment

Herp.. Why didn't I think of that? cheers, will accept in 3 mins

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.