2

When I use includes, aliasing in a select clause does not work but when I use joins it does...

#This works and I can do: voters.first.ad_ed
voters = Voter.joins(:voter_election_history).select("voters.ed, voters.ad, concat_ws('/', voters.ad, voters.ed) AS ad_ed, voter_election_histories.e110315").where("voter_election_histories.e110519 IS NULL").order('voters.ed')

#This query returns results but throws an undefined method error when I do voter.first.ad_ed
voters = Voter.includes(:voter_election_history).select("voters.ed, voters.ad, concat_ws('/', voters.ad, voters.ed) AS ad_ed, voter_election_histories.e110315").where("voter_election_histories.e110519 IS NULL").order('voters.ed').references(:voter_election_histories)

Why can't I access the aliased column in an includes query like this but I can do it with joins???

bkunzi01
  • 4,504
  • 1
  • 18
  • 25
  • 1
    pls, call `to_sql` on both queries and paste the resulting SQL queries – mrzasa Nov 08 '19 at 21:37
  • Using includes here does not really make sense. Since you're just selecting a single column on the joined table there is nothing to hydrate the records with if includes goes for a single query approach. – max Nov 08 '19 at 22:31
  • 2
    `includes` and `select` doesn't work together as expected. check this url for details: https://stackoverflow.com/questions/12400860/activerecord-includes-specify-included-columns – demir Nov 08 '19 at 22:56
  • Hey @max I removed some of the columns in the query to make it easier to focus on the part I need. It looks as though demir's link in the comment above discusses the same issue where they resort to adding new belongs_to relationships. I'll check it out! – bkunzi01 Nov 09 '19 at 17:12

0 Answers0