Getting Rails error "syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '('"

achilles77 picture achilles77 · Jun 14, 2014 · Viewed 14.7k times · Source

I am trying to migrate subject_id and course_id to the users table by typing:

rails generate migration add_course_id_and_subject_id_to_users course_id:integer, subject_id:integer

However, it results in the error:

_add_course_id_and_subject_id_to_users.rb:4: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '('

Not sure why this is happening.

Answer

awendt picture awendt · Jun 14, 2014

This is shell syntax, not Ruby syntax, so you need to drop the comma between you attribute defs:

rails generate migration add_course_id_and_subject_id_to_users course_id:integer subject_id:integer

With the comma, you're trying to add two fields, "course_id:integer," (comma!) and "subject_id:integer" with the types "integer," (comma!) and "integer", respectively.

Somewhere along the road, the generated Ruby code will have that syntax error and raise the exception.