[ActiveRecord] feature request - QueryMethods method that augments the select clause.

One little pet peeve of mine is that there is no way (afaik) to just add additional columns to the select clause of a query. Like for example if I’m joining and want a count or aggregate off the join table:

User.joins(:answers) .select(‘users.*’,‘AVG(answers.score) AS average_score’)

``

What I would really want to do is just add this to the existing select values of the scope:

User.joins(:answers) .select_also(‘AVG(answers.score) AS average_score’)

``

**Is this a worthwhile feature? **

I’ve had this too. I think I delegated it to select_with? Same behavior different name. I’m wondering what the most Rails-y name would be.

+1

I like it, but I don’t think it meets the criteria for inclusion in rails because it:

  • Could easily be a gem.

  • Is not meaningfully shorter.

    * Requires the next person to read this code to know what this
    

method does.

Its not so much that’s it’s shorter. They produce very different queries. Rails by default selects all the columns explicitly instead of using *.

If eager_load is used this becomes even more of a hassle since you also have to worry about selecting the the rows on the other table.

I really feel that there should be a built in method that matches the behavior of .where which does not clobber.

Sorry, I was being unclear there.

I think it might be sensible to convert .select(“users.*, sum(baz))” to the expanded form on the ruby side using the column cache. That way you get the functionality you want without introducing a new API.

Thanks, Daniel Heath