Rails ActiveRecord Model.uniq.pluck :id doesn't work with postgres Rails ActiveRecord Model.uniq.pluck :id doesn't work with postgres postgresql postgresql

Rails ActiveRecord Model.uniq.pluck :id doesn't work with postgres


This seems to be a problem with PostgreSQL. (Rails 3 DISTINCT QUERY)

To solve it, you could use select instead:

Collection.select([:name, :created_at]).order('created_at ASC').uniq.select(:name)

Or you could have Ruby get the names uniquely rather than SQL:

Collection.order('created_at ASC').pluck(:name).uniq


I was actually trying to solve this issue on active_admin. https://github.com/gregbell/active_admin/issues/2324

Now the solution seems to be Collection.reorder('name asc').uniq.pluck :name, this will overwrite the default_scope or previous order, and order collections with name. What's weird here is reorder works, while as order causes the problem...


Collection.select([:id, :created_at]).order('created_at ASC').uniq.pluck(:id)