Ruby on Rails
ReservedWords (Version #266)
Names You Can’t Use (aka reserved words, keywords) from Ruby and RubyOnRails
- ADDITIONAL_LOAD_PATHS
- ARGF
- ARGV
- ActionController
- ActionView
- ActiveRecord
- ArgumentError
- Array
- BasicSocket
- Benchmark
- Bignum
- Binding
- CGI
- CGIMethods
- CROSS_COMPILING
- Class
- ClassInheritableAttributes
- Comparable
- ConditionVariable
- Config
- Continuation
- DRb
- DRbIdConv
- DRbObject
- DRbUndumped
- Data
- Date
- DateTime
- Delegater
- Delegator
- Digest
- Dir
- ENV
- EOFError
- ERB
- Enumerable
- Errno
- Exception
- FALSE
- FalseClass
- Fcntl
- File
- FileList
- FileTask
- FileTest
- FileUtils
- Fixnum
- Float
- FloatDomainError
- GC
- Gem
- GetoptLong
- Hash
- IO
- IOError
- IPSocket
- IPsocket
- IndexError
- Inflector
- Integer
- Interrupt
- Kernel
- LN_SUPPORTED
- LoadError
- LocalJumpError
- Logger
- Marshal
- MatchData
- MatchingData
- Math
- Method
- Module
- Mutex
- Mysql
- MysqlError
- MysqlField
- MysqlRes
- NIL
- NameError
- NilClass
- NoMemoryError
- NoMethodError
- NoWrite
- NotImplementedError
- Numeric
- OPT_TABLE
- Object
- ObjectSpace
- Observable
- Observer
- PGError
- PGconn
- PGlarge
- PGresult
- PLATFORM
- PStore
- ParseDate
- Precision
- Proc
- Process
- Queue
- RAKEVERSION
- RELEASE_DATE
- RUBY
- RUBY_PLATFORM
- RUBY_RELEASE_DATE
- RUBY_VERSION
- Rake
- RakeApp
- RakeFileUtils
- Range
- RangeError
- Rational
- Regexp
- RegexpError
- Request
- RuntimeError
- STDERR
- STDIN
- STDOUT
- ScanError
- ScriptError
- SecurityError
- Signal
- SignalException
- SimpleDelegater
- SimpleDelegator
- Singleton
- SizedQueue
- Socket
- SocketError
- StandardError
- String
- StringScanner
- Struct
- Symbol
- SyntaxError
- SystemCallError
- SystemExit
- SystemStackError
- TCPServer
- TCPSocket
- TCPserver
- TCPsocket
- TOPLEVEL_BINDING
- TRUE
- Task
- Text
- Thread
- ThreadError
- ThreadGroup
- Time
- Transaction
- TrueClass
- TypeError
- UDPSocket
- UDPsocket
- UNIXServer
- UNIXSocket
- UNIXserver
- UNIXsocket
- UnboundMethod
- Url
- VERSION
- Verbose
- YAML
- ZeroDivisionError
Other names that have been reported to cause trouble:
- accept
- action
- attributes – if you have a has_many called attributes, you can’t access to your object attributes anymore; only the associated objects
- application2
- @base_path – setting this variable name in a controller method seems to break the ablity to render a partial in the view. The view will render with no content and no errors will be generated .
- connection – there seems to be a connection class already
- database – (in mysql)
- dispatcher
- display1
- format
- key
- layout – If you have a model called Layout and in a controller have “scaffold :layout” it generates an exception. However, if you script/generate the scaffold for layout it works.
- load – When making an Ajax call to an action named load, the action’s code will be skipped (or otherwise rendered useless). This is made apparent by: a) @variables are not available in the view, b) calling render :layout => false still yields the layout.
- link – breaks migrations when used as a column name in combination with validation: ticket
- new, override to news if you want a news table
- notify – not a valid column name
- open – not a valid column name
- public
- quote ‘quote’ cannot be used as a column name
- render – cannot be used as an action name
- request
- records – a table named records seemed to cause duplicate entries to be found by find
- responses – scaffold borks with “undefined method ‘body=’ ”
- scope – do not use as an association name because ActiveRecord::Base.scope is called instead
- send
- session (session_controller or SessionController will not work)
- system – a table column named system causes problems when trying to generate scaffold
- template – a table named templates causes an error when you try to invoke the create method of the default controller
- test (however those work with ruby test/unit/axis_test.rb and rake test_units)
- timeout – an ActiveRecord attribute named timeout will clash with the global function “timeout” defined in Ruby’s timeout.rb
- to_s—naming a model instance method to_s resulted in ‘File not found’ for any view an object of this class (should have) appeared in (no matter which method called) and WebRick had to be restarted. I couldn’t drag the very cause into light, but in the traces ‘to_s’ gave me a hint. After renaming everything worked well again.
- type—or any of the other MagicFieldNames
- URI
- visits—a table column named visits causes problems when trying to query some_obj.visits.
- Observer—for a model name works in development environment but not in production.
singular names finishing in “s”: Axis → Axes, Access → Accesses, will break the pluralization in rake: Axi, Acces
Names You Can’t Use from SQL
The list of reserved words is dependent on the database you use, for portability reasons it would be wise to not chose a field name listed in any of these tables:
If you aren’t sure, you can check the word against the SQL Reserved Words Checker
Also note that numerous field names have special properties. See the full list of MagicFieldNames.
Typical Errors
The errors that occur when you use a reserved word tend to be very confusing. Things that you think are happening in your code, are actually happening somewhere in the framework. Sometimes you can look at the stack trace and see that its not going through your class, but through some framework class. If you have an error that makes no sense at all, I would check to make sure you don’t have a name that conflicts with the above list.
In one instance I got a mysql error when I tried to save a model that belongs_to :quote. The belongs_to made a method that overrode the quote method in activerecord::base, which caused Quote objects to be returned where activerecord was expecting a quoted string!
Requests
- Please explain the problems with ‘display’. I am using a controller called image, and if I define ‘display’ as an action method name, I get “Unknown action: No action responded to display.”
1 For action, the only problem comes into play when you have a model named Action and feed that to the form helpers. The problem is that the array action[field_names] in the params[] method will be overwritten by Rails. Instead of the form data, you will only get the Controller method, as in ‘create’ or ‘edit’.
By feeding a different word, such as action_data, to form helpers in place of the actual model name you can easily work around this problem. Then, simply access it as params[:action_data] instead of params[:action]. If you are experiencing this problem, you will probably receive the error undefined method `stringify_keys!' for "create":String.
2 Seen when trying to map legacy APPLICATION table to an Application ActiveRecord. Causes confusion in rails dynamic require between application.rb (app defaults) and application.rb (ActiveRecord model).
I’ve been having problems when I try to generate a scaffold for Applications. I’m trying to develop a web app that will manage software Applications; thus, I created an applications table in my database and generated a scaffold off of it. It doesn’t seem to work! Any ideas or is this just not possible?
For some reason I’ve been having problems with Base and pagination.
I found this page had been spammed, and so did a rollback to a good version.
I have a column name called ‘order’ in two of my db tables. I have just realized that it is causing some problems since ‘order’ is a reserved word in mysql. Unfortunately, trying to run a migration that includes… rename_column :table_name, :order, :sequence
results in a mysql error. Is there anyway way to run a successful migration that forces mysql to make this change?
Can someone confirm conflicts with a named route of
map.image
and the image_tag helper? That route seems to instruct Rails to call image_url when image_Tag is invoked, and it fails.
yes confirmed this does cause issues, I have experienced the same thing – jamiequint
Can anybody tell me what’s up with “service”? stuff.service.inspect works fine, but stuff.service.title throws an “error occurred while evaluating nil.title”
I also think this is the right place to make a note of conflicts that arise from column names and magic methods.
Take for example a column ‘date_and_time’ in your event table. This would not work together with the find_by_ dynamic finder.
In MySQL 4.x the word ‘fields’ is reserved, so a model named Field with the corresponding table ‘fields’ isn’t allowed (CREATE TABLE fails). In MySQL 5.x this word is no longer reserved.
Don’t use ‘inspect’ as a controller action.
Don’t use ‘label’ as an action name as of rails 2 (or thereabouts). ‘labels’ works fine. Using label allowed seemed to work partially but rails seemed to render the view prior to executing the code I had in my contoller’s action method.
Names You Can’t Use (aka reserved words, keywords) from Ruby and RubyOnRails
- ADDITIONAL_LOAD_PATHS
- ARGF
- ARGV
- ActionController
- ActionView
- ActiveRecord
- ArgumentError
- Array
- BasicSocket
- Benchmark
- Bignum
- Binding
- CGI
- CGIMethods
- CROSS_COMPILING
- Class
- ClassInheritableAttributes
- Comparable
- ConditionVariable
- Config
- Continuation
- DRb
- DRbIdConv
- DRbObject
- DRbUndumped
- Data
- Date
- DateTime
- Delegater
- Delegator
- Digest
- Dir
- ENV
- EOFError
- ERB
- Enumerable
- Errno
- Exception
- FALSE
- FalseClass
- Fcntl
- File
- FileList
- FileTask
- FileTest
- FileUtils
- Fixnum
- Float
- FloatDomainError
- GC
- Gem
- GetoptLong
- Hash
- IO
- IOError
- IPSocket
- IPsocket
- IndexError
- Inflector
- Integer
- Interrupt
- Kernel
- LN_SUPPORTED
- LoadError
- LocalJumpError
- Logger
- Marshal
- MatchData
- MatchingData
- Math
- Method
- Module
- Mutex
- Mysql
- MysqlError
- MysqlField
- MysqlRes
- NIL
- NameError
- NilClass
- NoMemoryError
- NoMethodError
- NoWrite
- NotImplementedError
- Numeric
- OPT_TABLE
- Object
- ObjectSpace
- Observable
- Observer
- PGError
- PGconn
- PGlarge
- PGresult
- PLATFORM
- PStore
- ParseDate
- Precision
- Proc
- Process
- Queue
- RAKEVERSION
- RELEASE_DATE
- RUBY
- RUBY_PLATFORM
- RUBY_RELEASE_DATE
- RUBY_VERSION
- Rake
- RakeApp
- RakeFileUtils
- Range
- RangeError
- Rational
- Regexp
- RegexpError
- Request
- RuntimeError
- STDERR
- STDIN
- STDOUT
- ScanError
- ScriptError
- SecurityError
- Signal
- SignalException
- SimpleDelegater
- SimpleDelegator
- Singleton
- SizedQueue
- Socket
- SocketError
- StandardError
- String
- StringScanner
- Struct
- Symbol
- SyntaxError
- SystemCallError
- SystemExit
- SystemStackError
- TCPServer
- TCPSocket
- TCPserver
- TCPsocket
- TOPLEVEL_BINDING
- TRUE
- Task
- Text
- Thread
- ThreadError
- ThreadGroup
- Time
- Transaction
- TrueClass
- TypeError
- UDPSocket
- UDPsocket
- UNIXServer
- UNIXSocket
- UNIXserver
- UNIXsocket
- UnboundMethod
- Url
- VERSION
- Verbose
- YAML
- ZeroDivisionError
Other names that have been reported to cause trouble:
- accept
- action
- attributes – if you have a has_many called attributes, you can’t access to your object attributes anymore; only the associated objects
- application2
- @base_path – setting this variable name in a controller method seems to break the ablity to render a partial in the view. The view will render with no content and no errors will be generated .
- connection – there seems to be a connection class already
- database – (in mysql)
- dispatcher
- display1
- format
- key
- layout – If you have a model called Layout and in a controller have “scaffold :layout” it generates an exception. However, if you script/generate the scaffold for layout it works.
- load – When making an Ajax call to an action named load, the action’s code will be skipped (or otherwise rendered useless). This is made apparent by: a) @variables are not available in the view, b) calling render :layout => false still yields the layout.
- link – breaks migrations when used as a column name in combination with validation: ticket
- new, override to news if you want a news table
- notify – not a valid column name
- open – not a valid column name
- public
- quote ‘quote’ cannot be used as a column name
- render – cannot be used as an action name
- request
- records – a table named records seemed to cause duplicate entries to be found by find
- responses – scaffold borks with “undefined method ‘body=’ ”
- scope – do not use as an association name because ActiveRecord::Base.scope is called instead
- send
- session (session_controller or SessionController will not work)
- system – a table column named system causes problems when trying to generate scaffold
- template – a table named templates causes an error when you try to invoke the create method of the default controller
- test (however those work with ruby test/unit/axis_test.rb and rake test_units)
- timeout – an ActiveRecord attribute named timeout will clash with the global function “timeout” defined in Ruby’s timeout.rb
- to_s—naming a model instance method to_s resulted in ‘File not found’ for any view an object of this class (should have) appeared in (no matter which method called) and WebRick had to be restarted. I couldn’t drag the very cause into light, but in the traces ‘to_s’ gave me a hint. After renaming everything worked well again.
- type—or any of the other MagicFieldNames
- URI
- visits—a table column named visits causes problems when trying to query some_obj.visits.
- Observer—for a model name works in development environment but not in production.
singular names finishing in “s”: Axis → Axes, Access → Accesses, will break the pluralization in rake: Axi, Acces
Names You Can’t Use from SQL
The list of reserved words is dependent on the database you use, for portability reasons it would be wise to not chose a field name listed in any of these tables:
If you aren’t sure, you can check the word against the SQL Reserved Words Checker
Also note that numerous field names have special properties. See the full list of MagicFieldNames.
Typical Errors
The errors that occur when you use a reserved word tend to be very confusing. Things that you think are happening in your code, are actually happening somewhere in the framework. Sometimes you can look at the stack trace and see that its not going through your class, but through some framework class. If you have an error that makes no sense at all, I would check to make sure you don’t have a name that conflicts with the above list.
In one instance I got a mysql error when I tried to save a model that belongs_to :quote. The belongs_to made a method that overrode the quote method in activerecord::base, which caused Quote objects to be returned where activerecord was expecting a quoted string!
Requests
- Please explain the problems with ‘display’. I am using a controller called image, and if I define ‘display’ as an action method name, I get “Unknown action: No action responded to display.”
1 For action, the only problem comes into play when you have a model named Action and feed that to the form helpers. The problem is that the array action[field_names] in the params[] method will be overwritten by Rails. Instead of the form data, you will only get the Controller method, as in ‘create’ or ‘edit’.
By feeding a different word, such as action_data, to form helpers in place of the actual model name you can easily work around this problem. Then, simply access it as params[:action_data] instead of params[:action]. If you are experiencing this problem, you will probably receive the error undefined method `stringify_keys!' for "create":String.
2 Seen when trying to map legacy APPLICATION table to an Application ActiveRecord. Causes confusion in rails dynamic require between application.rb (app defaults) and application.rb (ActiveRecord model).
I’ve been having problems when I try to generate a scaffold for Applications. I’m trying to develop a web app that will manage software Applications; thus, I created an applications table in my database and generated a scaffold off of it. It doesn’t seem to work! Any ideas or is this just not possible?
For some reason I’ve been having problems with Base and pagination.
I found this page had been spammed, and so did a rollback to a good version.
I have a column name called ‘order’ in two of my db tables. I have just realized that it is causing some problems since ‘order’ is a reserved word in mysql. Unfortunately, trying to run a migration that includes… rename_column :table_name, :order, :sequence
results in a mysql error. Is there anyway way to run a successful migration that forces mysql to make this change?
Can someone confirm conflicts with a named route of
map.image
and the image_tag helper? That route seems to instruct Rails to call image_url when image_Tag is invoked, and it fails.
yes confirmed this does cause issues, I have experienced the same thing – jamiequint
Can anybody tell me what’s up with “service”? stuff.service.inspect works fine, but stuff.service.title throws an “error occurred while evaluating nil.title”
I also think this is the right place to make a note of conflicts that arise from column names and magic methods.
Take for example a column ‘date_and_time’ in your event table. This would not work together with the find_by_ dynamic finder.
In MySQL 4.x the word ‘fields’ is reserved, so a model named Field with the corresponding table ‘fields’ isn’t allowed (CREATE TABLE fails). In MySQL 5.x this word is no longer reserved.
Don’t use ‘inspect’ as a controller action.
Don’t use ‘label’ as an action name as of rails 2 (or thereabouts). ‘labels’ works fine. Using label allowed seemed to work partially but rails seemed to render the view prior to executing the code I had in my contoller’s action method.