The simplest way to select all distinct values of a column is, somewhat unintuitively:
Visit.uniq.pluck(:project)
this runs the query select distinct project from visits
, and returns an array of strings. Exactly what you need, except ... I want it to be paginated. So we
Visit.uniq.pluck(:project).page(1)
... and that completely bombs: we now get an array of numbers?
So we try something else, and write:
Visit.select('distinct project')
which runs the good query, but returns an array of Visit
's with only the project filled in. I can live with that. And then pagination (using the kaminari gem) is again as expected:
Visit.select('distinct project').page(params[:page])
Nice :)
Comments
Add comment