# Downcasing Legacy Table Column Names

**URL:** <https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143>\
**Category:** Rails\
**Created:** [November 30, 2005, 5:43pm UTC](https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143 "2005-11-30T17:43:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [November 30, 2005, 5:43pm UTC](https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143/1 "2005-11-30T17:43:18Z")

</div>

I’m looking for the simplest way to _automatically_ downcase the  
attribute names of Models mapped to legacy tables that use upper  
case. eg:

class Workorder \< ActiveRecord::Base  
set\_table\_name “WORKORDER”  
end

which has “NAME” as an attribute can use “name” as an attribute.

currently:  
workorder = Workorder.find(1)  
workorder.NAME # =\> “workorder 1 name”

I want:  
workorder.name # =\> “workorder 1 name”

Thanks,  
Jamie

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [December 1, 2005, 3:24pm UTC](https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143/2 "2005-12-01T15:24:43Z")

</div>

You can add a getter/setter for each attribute like:

class Workorder \< ActiveRecord::Base  
set\_table\_name “WORKORDER”

def name  
self.NAME  
end

def name=(theName)  
self.NAME = theName  
end

end

I’ve never used this myself so I may have the syntax wrong. However I  
believe the concept is sound. Although it is not “automatic” as you  
would like.

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [December 1, 2005, 6:11pm UTC](https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143/3 "2005-12-01T18:11:16Z")

</div>

Yeah, but I didn’t want to hand code all those accessors–was looking  
for an automatic way to deal with the problem.

Thanks,

Jamie

---

<div class="post-metadata">

**Author:** ![jamie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aca169/32.png) [@jamie](https://www.ruby-forum.com/u/jamie)\
**Post date:** [December 1, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/downcasing-legacy-table-column-names/49143/4 "2005-12-01T15:56:51Z")

</div>

I ended up overriding method\_missing in an abstract Model class.  
Since ActiveRecord::Base’s method\_missing is private, I ended up  
copying the whole thing and putting the modified version in my  
subclass. Basically, since I know the column names are upper case, I  
just look to see if method\_name.upcase is in the model’s attributes  
hash and if so, upcase! the method\_name and pass it along.

I’ve documented this here:  
[http://blog.dangdev.com/articles/2005/11/30/activerecord-model-legacy-](http://blog.dangdev.com/articles/2005/11/30/activerecord-model-legacy-)  
table-column-names

Looking forward to comments or a better way to do this. (What I would  
really like is to convert the attributes to lower case as they are set.)

Jamie
