Forum: Ruby on Rails STI, subclasses and callbacks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
marcus (Guest)
on 2006-03-02 00:47
(Received via mailing list)
I have a STI class tree. I want to set some default values (calculated
values so I can't set it in the database as defaults) on every created
instance regardless of what subclass is actually instantiated. So I
figured adding a after_create callback in the top class in the hierarchy
should do the trick. It seems it doesn't get called :(

Code:
class SuperClass < ActiveRecord::Base
...
   after_create :set_defaults
...
   private
   def set_defaults
     puts "Setting defaults #{id}"
     ... set values ...
     save()
   end
end

Am I doing something wrong here?

/Marcus
marcus (Guest)
on 2006-03-02 00:59
(Received via mailing list)
I tried this one (that worked but it's not very pretty...)

   def SuperClass.inherited(sub_class)
     ...
     sub_class.class_eval <<-EOS
       after_create { |node| ... set defaults ... ; node.save }
     EOS
     ...
   end

I'm doing work in the inherited department anyway so it's not that bad.
But I thought that callbacks should work over subclasses. Don't they?

/Marcus
Rick O. (Guest)
on 2006-03-02 01:35
(Received via mailing list)
On 3/1/06, marcus <removed_email_address@domain.invalid> wrote:
> I'm doing work in the inherited department anyway so it's not that bad.
> But I thought that callbacks should work over subclasses. Don't they?
>
> /Marcus

It's a slightly tricky subject, but I go over it on my blog:

http://weblog.techno-weenie.net/2006/2/21/unitiali...

Quick tips:

- don't override self.inherited
- put validations/callbacks ABOVE associations (especially in Rails 1.0)


--
Rick O.
http://techno-weenie.net
marcus (Guest)
on 2006-03-02 02:13
(Received via mailing list)
Rick O. skrev:
> It's a slightly tricky subject, but I go over it on my blog:
>
> http://weblog.techno-weenie.net/2006/2/21/unitiali...

How does this article relate to the subject? I see you mention STI there
but it's in the context of Reloadable classes.

> Quick tips:
>
> - don't override self.inherited

Why? Is Rails doing something there as well? How do I do if I want to do
something like generate methods based on my own conventions when
subclassed (in this particular class hierarchy)?

> - put validations/callbacks ABOVE associations (especially in Rails 1.0)

Okay, I'll try that.

Thanks

/Marcus
Rick O. (Guest)
on 2006-03-02 16:46
(Received via mailing list)
On 3/1/06, marcus <removed_email_address@domain.invalid> wrote:
> >
> Thanks
>
> /Marcus

Ha, I'm sorry, wrong article.  THIS one is the one i was thinking of:

http://weblog.techno-weenie.net/2006/2/15/activere...

It explains the self.inherited bit.

--
Rick O.
http://techno-weenie.net
This topic is locked and can not be replied to.