Reserved words in Rails
Oh the agony… I just spent half a day agonising over a new model that I created, and didn’t work when I was doing a find for a record. Just because one of the column name is a named after a reserved word! The error was an obscure:
SyntaxError: compile error
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.2/lib/active_record/base.rb:1359: syntax error, unexpected tINTEGER
Object::1
So I dedicate this blog entry to the listing of reserved words in Rails. If anyone else has come across any other reserved words that gave you hell, please comment!
Reserved words for Rails’ ActiveRecord
- type
- integer
- string
- text
- file
- number
Reserved words for Rails’ ActiveController
- template ( Someone mentioned this on the Internet. I didn’t test this out )
Updates:
Some people have pointed me to these links. They are more comprehensive!
Categories: Rails Reference, Ruby on Rails
Got the same mysterious error due to a column named ‘type’. Quite a common name for a column I would think. Anyway, thanks for posting this.
Unable to save a collection using a model with the name ‘Attribute’ when using the following model relationship
Class Person < ActiveRecord::Base
has_many :attributes
end
Doing this eventually fixed the problem:
Class Person “Attribute”
end
Not sure what happened above, the working fix should look like:
Class Person “Attribute”
end
Strange errors like these for columns named “notify”
FYI
Looks like the statement,
has_many :tasks
in your model will also reveal a reserved word…
FYI
Another one : “class”
It’s OK to generate a migration with a class attribute and apply the migration, but in the console, I can’t create an entry.
Another one: “send”. It is a reserved word because it is used in the Kernel (Kernel.send). It can not be used as a function name, otherwise we would override the Kernel Method. If you need it to describe some kind of message process, use a different word, for example “transmit”.
What’s stupid is that this is a problem for over 4 years! God forbid you might want to have a ‘type’ column in one of your tables.
this produces an error when used in a model:
scope :parent, joins(:roles_users).where(‘roles.role = ?’, ‘Parent’)
this does not:
scope :parentx, joins(:roles_users).where(‘roles.role = ?’, ‘Parent’)
I can’t find anywhere that “parent” is a reserved word–but i can easily imagine that it is.
I’m finding this give me an error when using the model
scope :parents, where(:kind => ‘parent’)
I had a ‘type’ attribute in one of my models. I got errors when trying to seed my database.