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
Module
maybe like so:Then just include that in your models as necessary. For further refinement I would provide an
alias_columns
class-level method that accepts parameters such as:include
or:exclude
so you can monkey-patchActiveRecord::Base
with said module but only callalias_columns
as needed per model.