Weird error when order by 'when'

In my appointments model, I want to order by a column called "when", which is supposed to house the appointment date.

So im my model file appointment.rb I did this...

class Appointment < ActiveRecord::Base   default_scope :order => 'when'   [...] end

and I got this error...

SQLite3::SQLException: near "when": syntax error: SELECT "appointments".* FROM "appointments" ORDER BY when

Yes. Google for "sqlite reserved words"...

http://www.sqlite.org/lang_keywords.html

I believe I ran into something similar once some time ago. I think that if you enclose the column name with quotes it might take it.

pepe wrote:

I believe I ran into something similar once some time ago. I think that if you enclose the column name with quotes it might take it.

Well, actually, it has to be enclosed with ticks and then with single or double quotes, like this...

default_scope :order => '`when`'

or

default_scope :order => "`when`"

Leonel *.* wrote:

pepe wrote:

I believe I ran into something similar once some time ago. I think that if you enclose the column name with quotes it might take it.

Well, actually, it has to be enclosed with ticks and then with single or double quotes, like this...

default_scope :order => '`when`'

or

default_scope :order => "`when`"

Actually, it's a better idea to use quote_column_name instead of literal quotes. This way, changing databases won't break your code (MySQL and SQLite use `` identifier quoting, PostgreSQL uses "" or nothing, MS SQL uses , but the quote_column_name method abstracts them all).

Best,

Actually, it's a better idea to use quote_column_name instead of literal quotes. This way, changing databases won't break your code (MySQL and SQLite use `` identifier quoting, PostgreSQL uses "" or nothing, MS SQL uses , but the quote_column_name method abstracts them all).

I didn't know about that one. Thanks Marnen.