Closed bblimke closed 4 years ago
@bblimke
I tested what pluck
method behaves in this case, and got the same error, too.
User.limit(1).group(:name).select('last_name, count(*) as total').pluck
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "users.id" must appear in the GROUP BY clause or be used in an aggregate function
It also returns all attributes instead of just id and first_name when calling pluck
with no argument.
User.limit(1).select(:id, :first_name).pluck
# => [1, 'first_name', 'test.email@example.com', nil, ...]
I would like to keep the behavior of pluck_all
being the same as pluck
. And the return value's format is the only difference between them. Sorry for the unnotified behavior changes.
You could try this:
User.limit(1).group(:last_name).pluck_all("last_name, count(*) as total")
or
users = User.limit(1).select(:id, :first_name)
users.pluck_all(*users.select_values)
Thank you for the quick reply @khiav223577
It make sense to keep pluck_all compatible with pluck. Not critical, but I wonder if there is a way to improve pluck to be more intuitive when used in combination with select. If pluck_all is to follow pluck, then it's a pluck issue though.
but I wonder if there is a way to improve pluck to be more intuitive when used in combination with select.
@bblimke I find an issue in rails that may relate to your question. See: https://github.com/rails/rails/issues/12447
You could also submit another issue in rails repository to discuss the behaviors of pluck
. :)
Here is how pluck_all behaves in 2.0.3 and how our system expected it to behave
This is how version 2.0.4 behaves which breaks things for us
@khiav223577 I guess this PR https://github.com/khiav223577/pluck_all/pull/40 caused it?