Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How do you select multiple fields with distinct values, and other non-distinct fields with them, all in one call with where and limit? I tried .pluck (which supports multiple fields in rails 4), .uniq (which didn't work in my case).
This is what worked for me, when used in the controller action
@models = Model.select('DISTINCT ON (field1,field2,field3) *') .where(id: params[:id]) .limit(100)
Add a comment
Here's a bit less verbose and more ActiveRecord-centered approach which should work not only for Postgres but for MySQL as well.
Model.select('field1,field2').distinct.where(field3: 'value').limit(10)
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.