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.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.
The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
1.3 Active Record as an ORM Framework Represent inheritance hierarchies through related models. Validate models before they get persisted to the database. Perform database operations in an object-oriented fashion.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With