Using DISTINCT with ActiveRecord

13,721
YourModelClass.select("DISTINCT start_time, end_time")

This will return objects that aren't strictly records but they will inherit from ActiveRecord::Base and you can do pretty much anything with those objects that doesn't write to the database.

http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields

Share:
13,721
recursive_acronym
Author by

recursive_acronym

Updated on June 19, 2022

Comments

  • recursive_acronym
    recursive_acronym almost 2 years

    I'm using ActiveRecord / RoR.

    My table looks like this:

    id (int), start_time(time), end_time(time), ...
    
    1, 20:00, 23:00
    
    2, 20:00, 23:00
    
    3, 16:00, 20:00
    
    4, 16:00, 23:00
    
    5, 20:00, 22:00
    
    6, 16:00, 20:00
    

    I need to return the records that have a combination distinct start_time + end_time combination.