Transient subclass of an ActiveRecord subclass

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

> 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

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

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