Skip to content
Advertisement

Rails order method; column named :order

I have a model Exercises and it has columns of :circuit and :order (among others). In a view, I am trying to order the Exercises first by :circuit and then by :order. When I use the following:

@schedule.exercises.order(:circuit).each do |exercise|

it works as expected. However, when I try to add the :order column:

@schedule.exercises.order(:circuit, :order).each do |exercise|

I get the following error:

SQLite3::SQLException: near "order": syntax error: SELECT "exercises".* FROM "exercises"  WHERE "exercises"."schedule_id" = 1 ORDER BY circuit, order

The same error also occurs when I pass the :order column alone:

@schedule.exercises.order(:order).each do |exercise|

SQLite3::SQLException: near "order": syntax error: SELECT "exercises".* FROM "exercises"  WHERE "exercises"."schedule_id" = 1 ORDER BY order

I’m assuming that this is because the column name (:order) is the same as the SQL method name (order). I’m wondering if there’s any way around this other than changing my column heading?

Thanks, Stuart

Advertisement

Answer

Changing the column name is the only sensible option out of this. Change it to something like “position”.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement