# Is there an elegant way to map column names

**URL:** <https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395>\
**Category:** Rails\
**Created:** [July 18, 2008, 2:06am UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395 "2008-07-18T02:06:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 18, 2008, 2:06am UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395/1 "2008-07-18T02:06:23Z")

</div>

I have been searching high and low but I can’t find an elegant way to  
map database column names to model names.

The idea is simple if I have a column named ‘CUSTOM\_5’ I want this to  
show up as "favorite\_pet’.

So far all the suggestions I have seen are to create your custom  
methods which is fine but a lot of work if you have to map most if not  
all your fields.

Also creating getters and setters means you now have to add those  
methods to the to\_xml and to\_json methods as well as excluding the  
original field names.

Surely there is an easier way to handle this very common problem.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 18, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395/2 "2008-07-18T18:52:45Z")

</div>

Could ruby’s ‘alias’ command work for this? Something like:

alias favorite\_pet custom\_5  
alias favorite\_pet= custom\_5=

Not sure where you’d put those lines though…

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 18, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395/3 "2008-07-18T20:18:50Z")

</div>

That’s not gonna work because those methods don’t really exist. You  
could probably extend AR::Base’s method\_missing method, but really,  
you should change the database columns’ names.

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 18, 2008, 9:01pm UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395/4 "2008-07-18T21:01:39Z")

</div>

This should work.

Usage:

class GreenPastures \< ActiveRecord::Base

alias\_column “new\_name” =\> “old\_nAmE”

end

Include this code in a file in /lib

module Legacy  
def self.append\_features(base)  
super  
base.extend(ClassMethods)  
end  
module ClassMethods  
def alias\_column(options)  
options.each do |new\_name, old\_name|  
self.send(:define\_method, new\_name) { self.send(old\_name) }  
self.send(:define\_method, “#{new\_name}=”) { |value|  
self.send("#{old\_name}=", value) }  
end  
end  
end  
end

ActiveRecord::Base.class\_eval do  
include Legacy  
end

---

<div class="post-metadata">

**Author:** ![Tim\_U](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90db22/32.png) [@Tim\_U](https://www.ruby-forum.com/u/Tim_U)\
**Post date:** [July 19, 2008, 3:05am UTC](https://www.ruby-forum.com/t/is-there-an-elegant-way-to-map-column-names/143395/5 "2008-07-19T03:05:54Z")

</div>

> class GreenPastures \< ActiveRecord::Base
> 
> alias\_column “new\_name” =\> “old\_nAmE”
> 
> end

I saw this during my search. I am wondering how this is different that  
simply using the ruby alias command.

Of course this won’t effect the “to\_xml” and “to\_json” methods. They  
seem to ignore the local attr.

I guess you’d have to build a hash, call this method for every member  
of the hash and finally pass those to the to\_xml method.
