2

While selecting a column name with a different name like so

=> t = TeamMapping.select('team_id as team').last
=> t[:team]
> 2

works. But if I add another table as an included and referenced in query, suddenly this behaviour breaks.

=> t = TeamMapping.includes(:user).references(:user).select('team_id as team').last
=> t[:team]
> nil

Please note that user table has no column whatsoever with the name team. I was not able to find anything relevant by searching on google. I would appreciate it if someone can explain what is happening behind the scenes.

Vedanshu
  • 617
  • 1
  • 7
  • 20

1 Answers1

0

Try this

select("team_mappings.team_id AS team")

or use joins instead

TeamMapping.joins(:user).select('team_mappings.team_id AS team').last    

Hope that helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78