# ActiveRecord column with reserved name 'class'

**URL:** <https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022>\
**Category:** rubyonrails-talk\
**Created:** [January 11, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022 "2008-01-11T09:40:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 11, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022/1 "2008-01-11T09:40:29Z")

</div>

I have a legacy database with a column named 'class' which is causing Rails problems. Renaming the column isn't an option and the workarounds I've seen so far don't suit.

I'm running rails 2.0.2 and an example of the error thrown looks like this... undefined method `generated\_methods' for "ADD":String

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/attribute\_methods.rb:188:in `method\_missing'

(The value returned in the 'class' column is 'ADD' in this case.

Can anyone suggest a nice, neat, dry, satisfying solution?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [January 11, 2008, 10:06am UTC](https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022/2 "2008-01-11T10:06:06Z")

</div>

> I have a legacy database with a column named 'class' which is causing Rails problems. Renaming the column isn't an option and the  
> workarounds I've seen so far don't suit.
> 
> I'm running rails 2.0.2 and an example of the error thrown looks like this... undefined method `generated\_methods' for "ADD":String
> 
> c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active\_record/ attribute\_methods.rb:188:in `method\_missing'

How about class MyTable \< AR:Base &nbsp;&nbsp;&nbsp;class \<\< self &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def instance\_method\_already\_implemented?(method\_name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if method\_name == 'class' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end end

If I'm reading things right, that will stop AR from generating a class  
method to read your attribute. You can still get at it via foo[:class]  
(or define you own method returning read\_attribute(:class)

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 11, 2008, 12:27pm UTC](https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022/3 "2008-01-11T12:27:19Z")

</div>

Frederick Cheung wrote:

> How about class MyTable \< AR:Base &nbsp;&nbsp;&nbsp;class \<\< self &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def instance\_method\_already\_implemented?(method\_name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if method\_name == 'class' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end end
> 
> If I'm reading things right, that will stop AR from generating a class method to read your attribute. You can still get at it via foo[:class] (or define you own method returning read\_attribute(:class)

You're absolutely right and that'll ding dang do for me very nicely thanks, Fred! I no longer have errors in all the places they were showing before.

Just as a note to anyone else reading this post, you might need to restart script/console rather than issue a reload! command after implementing this fix.

Derek

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 5:38pm UTC](https://discuss.rubyonrails.org/t/activerecord-column-with-reserved-name-class/23022/4 "2008-08-19T17:38:24Z")

</div>

The same thing worked for me as well. GORGEOUS!! AHHHHHHH!! I want to go run around my office now.

Thank you so much for the help!! AWESOME!
