Using DISTINCT with ActiveRecord

I am 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 records with a combination of the excellent combination start_time + end_time .

+6
source share
1 answer
 YourModelClass.select("DISTINCT start_time, end_time") 

This will return objects that are not strict records, but they inherit from ActiveRecord :: Base, and you can do almost anything with those objects that are not written to the database.

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

+18
source

Source: https://habr.com/ru/post/885568/


All Articles