# ActiveRecord Polymorphic Inheritance

**URL:** <https://www.ruby-forum.com/t/activerecord-polymorphic-inheritance/176755>\
**Category:** Rails\
**Created:** [October 5, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/activerecord-polymorphic-inheritance/176755 "2009-10-05T22:34:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/58f4c7/32.png) [@rubynewbie](https://www.ruby-forum.com/u/rubynewbie)\
**Post date:** [October 5, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/activerecord-polymorphic-inheritance/176755/1 "2009-10-05T22:34:10Z")

</div>

I am using RoR ActiveRecord polymorphic inheritance and was wondering  
if it’s possible to access the base class’ association methods in the  
invocation of either to\_xml or to\_json without having to depict the  
base class data in the serialized string. Here are my classes:

# base class

class Asset \< ActiveRecord::Base  
belongs\_to :resource, :polymorphic =\> true  
has\_many :right\_owners, :dependent =\> :destroy  
end

# derived class

class Song \< ActiveRecord::Base  
has\_one :asset, :as =\> :resource, :dependent =\> :destroy  
end

# derived class

class Film \< ActiveRecord::Base  
has\_one :asset, :as =\> :resource, :dependent =\> :destroy  
end

If I instantiate the asset, as in the case of asset = Asset.find  
(:first, :include=\>:resource), I can easily call asset.to\_xml  
(:include=\>:right\_owners).

However sometimes, we absolutely need to have the concrete class and  
not just the base class object or serialized state. If I instantiate  
the song, how can I serialize the underlying asset’s right\_owners data  
without introducing the ‘asset’ as the base class navigational tie?  
For example:

# instantiate the song

song = Song.find(:first, :include=\>:asset)

# serialize XML or JSON that includes the underlying asset’s

right\_owners

# I can do this but this but it is not compatible for our caller since

they don’t understand the nested base Asset #class  
song.to\_xml(:include=\>{:asset=\>{:include=\>:right\_owners}})  
#or alternatively for json:  
song.to\_json(:include=\>{:asset=\>{:include=\>:right\_owners}})

Is there a way to access and serialize the base class’ associative  
methods, i.e. ‘right\_owners’, without having to include the ‘asset’  
information. That is ideally, I’m looking for

 ..... ..... .... .... 

One workaround is to define the right\_owners relationship on the Song  
class but that feels redundant and doesn’t follow the DRY principle  
especially considering there will be other types of derived classes  
such as Films, etc…

If I define right\_owners as a method on the Song class, it just  
serializes the class name as an array rather treating it as a AR  
association that I can further navigate. That is a custom method is  
not being treated the same as the AR association method defined  
with :has\_one or :has\_many.

Any insight is much appreciated. Thanks.
