# Rails 2.2, abstract\_class, and associations

**URL:** <https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126>\
**Category:** Rails\
**Created:** [February 11, 2009, 11:10pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126 "2009-02-11T23:10:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 11, 2009, 11:10pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/1 "2009-02-11T23:10:12Z")

</div>

In my project, I’ve created an abstract model that my real models  
inherit from. In my abstract model, I’ve overridden method\_missing so  
that I can use attribute names without the field type prefix from the  
legacy database, without having to set up alias\_attribute for every  
attribute in every table.

class LegacyModel \< ActiveRecord::Base  
self.abstract\_class = true

def method\_missing(symbol, \*args)  
…do some stuff…  
end  
end

This all worked fine in Rails 2.0.2. It still works in Rails 2.2.2,  
but now not across a belongs\_to association. It does work across a  
has\_many association.

NoMethodError: NoMethodError  
from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active\_record/  
associations/association\_proxy.rb:209:in `method\_missing’

Anyone have any ideas on either a better way to handle the attribute  
names, or where to start looking to figure out how to make this work?

Thanks,

Jim Crate  
Que Viva, LLC

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 12, 2009, 12:11pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/2 "2009-02-12T12:11:53Z")

</div>

On 11 Feb 2009, at 22:09, Jim wrote:

> Anyone have any ideas on either a better way to handle the attribute  
> names, or where to start looking to figure out how to make this work?

Have you also overridden respond\_to?

Fred

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 12, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/3 "2009-02-12T16:38:54Z")

</div>

On Feb 12, 6:11 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Have you also overridden respond\_to?

No, I haven’t. But I’m curious what I would do with respond\_to? that  
would affect method\_missing?

Jim

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 13, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/4 "2009-02-13T20:43:51Z")

</div>

On Feb 12, 2:13 pm, Jim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Although, I have to wonder if there’s a better way to  
> accomplish this than through method\_missing. Maybe there’s some way  
> to loop through all attributes and call alias\_attribute dynamically  
> when classes are loaded?

There is - try this:

class LegacyModel \< ActiveRecord::Base  
column\_names.each do |c|  
# figure out the new name  
alias\_attribute new\_name, c  
end  
end

–Matt J.

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 17, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/5 "2009-02-17T20:45:13Z")

</div>

On Feb 13, 2:43 pm, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> column\_names.each do |c|  
> # figure out the new name  
> alias\_attribute new\_name, c  
> end  
> end
> 
> –Matt J.

This would work in a regular model, but not a model with  
abstract\_class set to true. LegacyModel is an abstract class that  
other models inherit from, so I do not have to repeat certain things  
which apply to all legacy tables in the database, like setting the  
primary key, certain alias\_attribute calls, and a few other things.

Jim

---

<div class="post-metadata">

**Author:** ![jim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@jim](https://www.ruby-forum.com/u/jim)\
**Post date:** [February 12, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/rails-2-2-abstract-class-and-associations/160126/6 "2009-02-12T20:14:12Z")

</div>

On Feb 12, 6:11 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Have you also overridden respond\_to?

I played around with overriding respond\_to?, and figured out how to  
make this all work. Apparently, in Rails 2.2, if respond\_to? returns  
false, method\_missing gets called on the association proxy instead of  
the base class. Although, I have to wonder if there’s a better way to  
accomplish this than through method\_missing. Maybe there’s some way  
to loop through all attributes and call alias\_attribute dynamically  
when classes are loaded?

Anyway, thanks for the help.

Jim
