jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

No way to use "Distinct" within scopes with Postgresql #160

Open pkb4112 opened 6 years ago

pkb4112 commented 6 years ago

I understand as per the documentation (http://filterrific.clearcove.ca/pages/active_record_scope_patterns.html) that using "DISTINCT" within scopes for Filterrific causes it to break because it conflicts with the default ORDER BY clauses that Filterrific includes. (ex. ActionView::Template::Error (PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list )

Is there no workaround for this? I really need a way to display distinct records within the Filterrific list on my view. Is it possible to disable or overwrite the default query completely to keep it from conflicting with PostgreSQL?

All I'm trying to accomplish is below:

scope :checked_out, -> { joins(:checked_out_items).distinct }

Rails 5 Postgresql

tkz79 commented 6 years ago

If i'm on the same page as you, I don't think there is a work around as it is a limitation of postgres that's specifically stated in their docs... The distinct call needs order by to match the left most order bys or you get that error. For distinct to work, you can't change the ordering without making a new query. https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT

Hard to tell without knowing the full story on what you want to do, but from the little I can see I think 'group by' is the bastard you're looking for...