# Transient subclass of an ActiveRecord subclass

**URL:** <https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513>\
**Category:** Rails\
**Created:** [September 4, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513 "2006-09-04T20:24:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 4, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513/1 "2006-09-04T20:24:10Z")

</div>

hey all

I posted this same topic yesterday and it never showed up on the group?  
I’ve heard reports of other peoples messages not showing up too…  
very annoying!

Anyway, I’d like to be able to have transient (no persistent data of  
their own) subclasses of my ActiveRecord classes but that seems to be  
problematic.

Basically I’d like to be able to do this…

class Film \< ActiveRecord::Base  
…  
…  
end

class SomeTransient \< Film  
…  
…  
end

class SomeOtherTransient \< Film  
…  
…  
end

etc…

Then do this:

film = SomeTransient.find\_by\_title(“some film title”)

This just struck me actually… are ruby class methods not inheritable?

Clearly I could do all my stuff directly in the Film class but that  
would get messy since the film instances are going to be taking on many  
different personalities (lots of different derived attributes for  
each).

I’m not sure why this seems to be so difficult?

Thanks in advance!  
Tim

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 12:20am UTC](https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513/2 "2006-09-05T00:20:31Z")

</div>

\> end \> film = SomeTransient.find\_by\_title("some film title") \> \> This just struck me actually... are ruby class methods not inheritable? \> \> Clearly I could do all my stuff directly in the Film class but that \> would get messy since the film instances are going to be taking on many \> different personalities (lots of different derived attributes for \> each). \>

I think you may be mixing up the semantics of inheritance here.  
Inheritance is a “is a kind of” relationship. Unfortunately, you  
haven’t posted enough code to work out what exactly you are trying to  
do, but I suspect it might be a case for delegation (which is a “uses”  
relationship).

With inheritance, you are asserting that an instance of an inherited  
class “is a” SomeTransient, and is also a Film. Now, if I understand  
correctly, you want a Film to _behave_ as a SomeTransient or  
SomeOtherTransient, depending on what it is being used for in your  
business logic.

Try:

class Film  
attr\_accessor :name  
end

require ‘delegate’  
class SomeTransient \< SimpleDelegator  
def name\_reverse  
self.name.reverse  
end  
end

You can now do:

f = Film.new  
f.name=“Hello”

s = SomeTransient.new(f)  
s.name\_reverse #=\> “olleH”

Or in your case, s = SomeTransient.new( Film.find\_by\_title(‘some file  
title’))

The Delegate library takes care of delegating all methods that are not  
explicitly defined in the Delegate class to the delegate Film.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 5, 2006, 4:38am UTC](https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513/3 "2006-09-05T04:38:29Z")

</div>

unknown wrote:

> Basically I’d like to be able to do this…
> 
> class Film \< ActiveRecord::Base  
> …  
> …  
> end
> 
> class SomeTransient \< Film  
> …  
> …  
> end
> 
> class SomeOtherTransient \< Film  
> …  
> …  
> end

Do you want single table inheritance? Or should Film have no table, but  
its children each have tables of their own?

If you want STI, just add a type field (string) to your film table and  
active record takes care of the rest by putting the name of the subclass  
in that field and then creating an instance of the right class when its  
read back out.

If you want pure ruby inheritance and each sub class has its own table  
then Film must be an abstract\_class:

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

class Foo \< Film  
#uses table ‘foos’ but has all methods of Film  
end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 8, 2006, 2:36am UTC](https://www.ruby-forum.com/t/transient-subclass-of-an-activerecord-subclass/67513/4 "2006-09-08T02:36:30Z")

</div>

Thanks for the respones guys, sorry it took me so long to get back but  
“self.abstract\_class = true” is, I think, what I was looking for:-)

Best  
Tim
