# STI, subclasses and callbacks

**URL:** <https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346>\
**Category:** Rails\
**Created:** [March 1, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346 "2006-03-01T23:47:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [March 1, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346/1 "2006-03-01T23:47:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [March 1, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346/2 "2006-03-01T23:59:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [March 2, 2006, 12:35am UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346/3 "2006-03-02T00:35:31Z")

</div>

On 3/1/06, marcus [removed\_email\_address@domain.invalid](mailto: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/unitialized-constant-technoweenie](http://weblog.techno-weenie.net/2006/2/21/unitialized-constant-technoweenie)

Quick tips:

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

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [March 2, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346/4 "2006-03-02T15:46:46Z")

</div>

On 3/1/06, marcus [removed\_email\_address@domain.invalid](mailto: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/activerecord-callbacks-and-sti](http://weblog.techno-weenie.net/2006/2/15/activerecord-callbacks-and-sti)

It explains the self.inherited bit.

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![marcus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@marcus](https://www.ruby-forum.com/u/marcus)\
**Post date:** [March 2, 2006, 1:13am UTC](https://www.ruby-forum.com/t/sti-subclasses-and-callbacks/54346/5 "2006-03-02T01:13:18Z")

</div>

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/unitialized-constant-technoweenie](http://weblog.techno-weenie.net/2006/2/21/unitialized-constant-technoweenie)

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
