Possible Duplicate:
Alias for column names in Rails
I'm writing a module of ActiveRecord models to access a MySQL legacy database. My problem is that the database has a column naming convention that looks like this: groupID, metaGroupName, flagName. Is there an easy way to make the models play nice with this naming convention?
Update:
I'd like to avoid the alias_attribute method if possible. It's not a small database, and that would be a lot of columns to alias. Is there a method in ActiveRecord that interprets column names that I can override, perhaps?
You don't want to keep repeating yourself—that's good. Fortunately, Ruby makes it rather easy. Try something like the following:
I'd then factor that out into a
Modulemaybe like so:Then just include that in your models as necessary. For further refinement I would provide an
alias_columnsclass-level method that accepts parameters such as:includeor:excludeso you can monkey-patchActiveRecord::Basewith said module but only callalias_columnsas needed per model.