Bruno
Bruno

Reputation: 6449

Order by time of day and regardless of date

I have a Schedule model with attributes Day and Time. Time is of datatype time.

I have data i.e.

Monday / 2014-04-26 15:00:00.000000

Monday / 2014-04-25 16:30:00.000000

In my controller I have @schedule = Schedule.all(:order => 'Day, Time')

Right now it is ordering by date, how do I order by time regardless of date?

Upvotes: 7

Views: 980

Answers (2)

Bruno
Bruno

Reputation: 6449

This is a SQLite3 Ruby connector issue. Works fine on Postgres.

Looks like SQLite does not have a native understanding of the Time datatype as stated here: Rails Active Record find(:all, :order => ) issue and here: http://www.sqlite.org/datatype3.html

Renaming the attribute from time to i.e. class_time did not solve the problem as recommended here: Sqlite3 activerecord :order => "time DESC" doesn't sort

Upvotes: 0

Eyeslandic
Eyeslandic

Reputation: 14900

If you're using Postgres you can use

@schedule = Schedule.all.order('date_time_field::time').

If you're on Mysql I think it's something like

DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY

Sqlite

@schedule = Schedule.all.order('time(date_time_field)')

Upvotes: 4

Related Questions