Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.