Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 807
Stuart Avatar asked Feb 20 '23 22:02

Stuart


1 Answers

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

like image 110
Ryan Bigg Avatar answered Feb 24 '23 09:02

Ryan Bigg