# Polymorphic has\_many from ActiveRecord::Base?

**URL:** <https://www.ruby-forum.com/t/polymorphic-has-many-from-activerecord-base/129809>\
**Category:** Rails\
**Created:** [February 21, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/polymorphic-has-many-from-activerecord-base/129809 "2008-02-21T18:44:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [February 21, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/polymorphic-has-many-from-activerecord-base/129809/1 "2008-02-21T18:44:33Z")

</div>

I have a polymorphic thingy called fields and I need to on each and  
every model, so I’d like to do the has\_many from the base class, like  
this…

ActiveRecord::Base.class\_eval do  
has\_many :fields, :as =\> :model, :dependent =\> true  
end

It fails with this no method error (class\_of\_active\_record\_descendant)

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_record/  
base.rb:1369:in `class_of_active_record_descendant':NoMethodError: undefined method`abstract\_class?’ for Object:Class

Is there a better way to do this???

---

<div class="post-metadata">

**Author:** ![uncle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/e5b9ba/32.png) [@uncle](https://www.ruby-forum.com/u/uncle)\
**Post date:** [February 22, 2008, 1:00am UTC](https://www.ruby-forum.com/t/polymorphic-has-many-from-activerecord-base/129809/2 "2008-02-22T01:00:36Z")

</div>

Instead of doing a class\_eval, have you tried this:

ActiveRecord::Base  
has\_many :fields, :as =\> :model, :dependent =\> true  
end

If you put this in a some\_name.rb file in your models, the AR::Base  
class will be extended with the functionality, which will then be  
inherited by your models. It might work…

Brent
