Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Record query causing SQLException near "," syntax error

I'm trying to display all such rows of a table STUDENTS which have a value of an attribute, such as COURSE IN a set of given values e.g. 'MS', 'PhD'.

I get the values in the students_controller.rb file using params. I tried to run an Active Record query using where to do the job:

@all_courses = ['MS', 'PhD', 'BA', 'MSc']

@students = Student.where("course IN :courses" , {:courses => params.has_key?(:courses) ? params[:courses].keys : @all_courses})

But I get the following error:

SQLite3::SQLException: near ",": syntax error: SELECT "students".* FROM "students"  WHERE (course IN 'MS', 'PhD', 'BA', 'MSc')

I think the error might be due to the absence of ; at the end of the SQL query generated by Active Record, but I cannot do anything to get that semicolon at the end.

like image 291
Arpit Chauhan Avatar asked Dec 03 '25 17:12

Arpit Chauhan


1 Answers

You need to use parentheses: "course IN (:courses)"

like image 120
James Avatar answered Dec 06 '25 06:12

James