ActiveRecord: mapping column names when using legacy schemat

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
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
column names being visible anymore.

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

e.g.: given legacy schema:

Table _fix has columns:
_ change

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

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 :slight_smile:

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
which does the above mapping?



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs