I have a model class named Event, and a field ‘when’. I’m trying to return events order by when desc, so here is the code.
@events = Event.order("'when' DESC")
I’m using MySQL as the database. but the returned results is not sorted.
I’ve looked into the console, and file the generated sql, which seems ok
SELECT `events`.* FROM `events` ORDER BY 'when' DESC
and I also run this sql in console, and it returned sorted result. Is anything special with ‘when’?
if I changed my query to Event.order("events.when DESC"), then it returned a sorted result
it should be backtick, not single quote.
when you use
single quotearoundwhenit is not a column anymore but merely a string value. you should have use backtick instead of single when escaping tableName and columnName.