Posts [ 5 ]

Topic: "Illegal" field names

I'm developing an application that has to connect to a legacy database. I have no control over the structure of this database, but the Rails application has to read and write to certain tables in that database.

So I've setup the two connections, and everything works fine. Except the one table kept on giving me an error when I tried to save to it. Either a new record, or an existing record. It kept on complaining about

"ArgumentError: wrong number of arguments (1 for 0)"

in the callback function of ActiveRecord. This happened whenever the Model attempted to validate the record.

So I finally figured out that the table in the Legacy database has a field called "callback" (It's a telephony app). So when Rails created the ORM structure it overrides it's own "callback" method with a new one mapped to the field called "callback".

The only way I could think of around the problem, was not to map that field. Luckily I don't need to use that field for my app. So I put the following into my Model:

This code comes out of the MySQL adapter for ActiveRecord. I added this bit:

unless field[0] == 'callback'

This does the trick very well, but I feel it's a bit of a dirty fix to the problem. It certainly makes upgrading to a new version of Rails (or ActiveRecord) problematic, as I'll have to make sure to check that specific method to see if it needs updating.

Is there a better way to let ActiveRecord not map a certain field in a table? Or even better, give a field a method name that's different from the field name?