# ActiveRecord column with reserved name 'class'

**URL:** <https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705>\
**Category:** Rails\
**Created:** [January 11, 2008, 10:40am UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705 "2008-01-11T10:40:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [January 11, 2008, 10:40am UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705/1 "2008-01-11T10: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:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [January 11, 2008, 11:07am UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705/2 "2008-01-11T11:07:08Z")

</div>

On 11 Jan 2008, at 09:40, Derek M. wrote:

> 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  
class \<\< self  
def instance\_method\_already\_implemented?(method\_name)  
return true if method\_name == ‘class’  
super  
end  
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:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [January 11, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705/3 "2008-01-11T13:27:19Z")

</div>

Frederick C. wrote:

> How about  
> class MyTable \< AR:Base  
> class \<\< self  
> def instance\_method\_already\_implemented?(method\_name)  
> return true if method\_name == ‘class’  
> super  
> end  
> 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:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [February 22, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705/4 "2009-02-22T20:47:57Z")

</div>

Worked great for me as well. Thanks!

---

<div class="post-metadata">

**Author:** ![lofye](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bb73d2/32.png) [@lofye](https://www.ruby-forum.com/u/lofye)\
**Post date:** [August 19, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/activerecord-column-with-reserved-name-class/125705/5 "2008-08-19T19: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!
