Order by time of day and regardless of date

I have a schedule model with Day and Time attributes. Time has a data type.

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')

Now he is ordering by date, how do I order by time, regardless of the date?

+4
source share
2 answers

This is a problem with the SQLite3 Ruby connector. Works great on Postgres.

It seems that SQLite does not have its own idea of ​​the Time data type, as indicated here: Rails Active Record find (: all ,: order =>) issue and here: http://www.sqlite.org/datatype3.html

time i.e. class_time , : Sqlite3 activerecord: order = > " DESC"

0

Postgres,

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

Mysql, , -

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

Sqlite

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

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


All Articles