# Need help verifying AR after\_xxx callback behaviors

**URL:** <https://www.ruby-forum.com/t/need-help-verifying-ar-after-xxx-callback-behaviors/74253>\
**Category:** Rails\
**Created:** [November 2, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/need-help-verifying-ar-after-xxx-callback-behaviors/74253 "2006-11-02T21:52:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 2, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/need-help-verifying-ar-after-xxx-callback-behaviors/74253/1 "2006-11-02T21:52:45Z")

</div>

I just found out the hard way that after\_initialize is called after both  
a “new” call on an AR object as well as a “find” call. So I’d like to  
summarize my understanding of these and have someone tell me if I’m  
correct.

Given an AR descendant “X”

after\_create: Gets called only after X.new  
after\_initialize: Gets called after either X.new and X.find  
after\_find: Gets called only after X.find

1. Is this correct?

2. Does after\_initialize only exist so that you don’t have to specify  
the same method in two callbacks (after\_create and after\_find)?

FYI, after\_create behavior can also be achieved by directly overriding  
the initialize method in your AR object.

I find the name “after\_initialize” confusing, but that’s another story  
…

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 2, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/need-help-verifying-ar-after-xxx-callback-behaviors/74253/2 "2006-11-02T22:29:13Z")

</div>

OK, I’ll correct myself.

after\_create is called after a call to X.create. That makes sense.

after\_initialize is called after both X.new and X.find. The fact that  
it’s called after find is totally annoying since I may just want to do  
_object initialization_ for X.

It would seem that to do object initialization that only gets called as  
part of a call to X.new, you have two choices:

1. Override the AR initialize(attributes = nil) method with your own and  
do you initialization there.

(NOTE: For people who have been doing OO for a long time, this can be  
somewhat confusing since one would think that initialize would also be  
called after X.find - but of course, it isn’t. ActiveRecord manages  
it’s initialization in a custom way).

1. Implement after\_initialize and wrap all of your data initialization  
in a if self.new\_record? block. While this is less intuitive to  
long-time OOers, it does respect the AR API more and isn’t bound to the  
current implementation of the initialize method in AR::Base.

#2 is more API respectful, but #1 will perform better and feels more  
natural.

What do other people do for data initialization?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [November 2, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/need-help-verifying-ar-after-xxx-callback-behaviors/74253/3 "2006-11-02T23:35:51Z")

</div>

One more thing:

I think that this would all make a lot more sense if two things were in  
place:

1. after\_initialize and after\_find were mutually exclusive so that  
after\_initialize would only get called on a new.

[AND, possibly]

1. a self-defined initialize method in an AR object were never called.

Then there might be less confusion (at least for me ;]).

Thanks,  
Wes
