# ActiveRecord: mapping column names when using legacy schemat

**URL:** <https://www.ruby-forum.com/t/activerecord-mapping-column-names-when-using-legacy-schemat/93062>\
**Category:** Rails\
**Created:** [April 27, 2007, 1:47am UTC](https://www.ruby-forum.com/t/activerecord-mapping-column-names-when-using-legacy-schemat/93062 "2007-04-27T01:47:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tilo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed8c4c/32.png) [@tilo](https://www.ruby-forum.com/u/tilo)\
**Post date:** [April 27, 2007, 1:47am UTC](https://www.ruby-forum.com/t/activerecord-mapping-column-names-when-using-legacy-schemat/93062/1 "2007-04-27T01:47:51Z")

</div>

I have an ActiveRecord question…

I have a legacy MySQL database with a weird naming convention - all  
table and column names have a prefix ‘\_’ , which is annoying, and  
some  
of the column names are plain misnomers.

I would like to apply a pre-defined mapping between MySQL column names  
and method names to be used in the Ruby class, without having the  
underlying  
column names being visible anymore.

How can I pre-define a static mapping of all (legacy) column names to  
new  
Class method names?

e.g.: given legacy schema:

Table \_fix has columns:  
\_job  
\_ change  
\_date  
\_effect

When defining the Ruby class, I’d like to do something like this:

class Fix \< ActiveRecord::Base  
self.table\_name = “\_fix”  
use\_mapping ( # imaginary construct…  
:\_job =\> “job\_id”,  
:\_change =\> “change\_id” ,  
:\_date =\> “date” ,  
:\_effect =\> “status”)

self.primary\_key = “job\_id” # no more reference to \_job column  
end

and want to access it like this:

Fix.find( id ).status = “partial fix” # should update the column  
\_effect in table \_fix

# and i want to see this 🙂

Fix.find( id ).\_job  
NoMethodError: undefined method ‘\_job’ for …  
Fix.find( id ).\_effect  
NoMethodError: undefined method ‘\_effect’ for …

The schema of the underlying legacy database should not be modified in  
any way.

Is there a way to do this? Is there a way to code a helper method for  
ActiveRecord::Base  
which does the above mapping?

thanks

Tilo
