Extending an active record model with obj.extend(Module)

Hi everyone,

I am trying to extend an active record model using obj.extend(module).
The aim is to overwrite some of the model methods depending on what’s in
the object. For example, the base class would be “file”, with objects
such as “image”, and “text”.

The aim is to be able to something like this:
file = File.new()
file.extend(Image)
file.process <-- does image processing from the Image module

or:
file = File.new()
file.extend(Text)
file.process <-- does text processing from the Text module

I’m having trouble working out where the file.extend(xxxxx) should go -
I’d like to be able to load the object, check it’s type, and then extend
with the appropriate module. I’ve tried using initialize in the
activerecord class, but it doesn’t seem to be called when a new model
instance is created. Any ideas?

thanks,
Dan

dan greig wrote:

I’ve tried using initialize in the
activerecord class, but it doesn’t seem to be called when a new model
instance is created. Any ideas?

Answering my own question - is that like talking to myself?? Anyway, the
after_initialize callback did the trick. Did I totally miss it in the
API or am I right that it’s not listed? (found it in AWDWR)

Be aware that using after_initialize will slow things down
substantially, especially when creating lots of objects.

-Jonathan.