# ActiveRecord: mapping column names when using legacy schemata

**URL:** <https://discuss.rubyonrails.org/t/activerecord-mapping-column-names-when-using-legacy-schemata/12216>\
**Category:** rubyonrails-talk\
**Created:** [April 26, 2007, 11:47pm UTC](https://discuss.rubyonrails.org/t/activerecord-mapping-column-names-when-using-legacy-schemata/12216 "2007-04-26T23:47:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tilo](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@tilo](https://discuss.rubyonrails.org/u/tilo)\
**Post date:** [April 26, 2007, 11:47pm UTC](https://discuss.rubyonrails.org/t/activerecord-mapping-column-names-when-using-legacy-schemata/12216/1 "2007-04-26T23:47:03Z")

</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: &nbsp;&nbsp;&nbsp;\_job &nbsp;&nbsp;&nbsp;\_ change &nbsp;&nbsp;&nbsp;\_date &nbsp;&nbsp;&nbsp;\_effect

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

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

&nbsp;&nbsp;&nbsp;self.primary\_key = "job\_id" # no more reference to \_job column end

and want to access it like this:

&nbsp;&nbsp;&nbsp;Fix.find( id ).status = "partial fix" # should update the column \_effect in table \_fix

&nbsp;&nbsp;&nbsp;# and i want to see this 🙂

&nbsp;&nbsp;&nbsp;Fix.find( id ).\_job &nbsp;&nbsp;&nbsp;&nbsp;NoMethodError: undefined method '\_job' for ... &nbsp;&nbsp;&nbsp;Fix.find( id ).\_effect &nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;Tilo
