dan greig
2006-Jul-11 01:07 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
dan greig
2006-Jul-11 06:50 UTC
[Rails] Re: extending an active record model with obj.extend(Module)
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) -- Posted via http://www.ruby-forum.com/.
Jonathan Viney
2006-Jul-11 08:15 UTC
[Rails] Re: extending an active record model with obj.extend(Module)
Be aware that using after_initialize will slow things down substantially, especially when creating lots of objects. -Jonathan. On 7/11/06, dan greig <dgreig@magian.com> wrote:> 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) > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >