Using Reserved Words in your Database Tables
Posted by g. | Filed under Ruby on Rails
I was banging my head against the wall the other day for several hours wondering why an extremely simple MyModel.find(:all) call was giving me the error: ArgumentError: wrong number of arguments (0 for 1).
There was absolutely nothing strange about the table, my controller, or model object. The next day it dawned on me, I thought “I bet one of the column names in my table is a reserved word”. Well, that turned out to be precisely the problem. One of my columns was named type, and that completely threw off ActiveRecord. I also think you cannot name a column open. I’m not sure if there is a complete list of reserved words somewhere, but double-check anytime you name a column something that sounds like it COULD be a reserved word. It could save you hours of mindless troubleshooting!
EDIT:
OK, so here is a list of reserved words that could help: List of Reserved Words in Ruby on Rails
Also, it would be nice to be able to alias columns in your models. In my case, I can’t just change the column name from type to something else, because I’m wrapping an existing table that cannot be altered. If anyone has an elegant solution, feel free to comment!
Tags: database, MySQL, rails, reserved words, table