Why no call to Model.new on find?


#1

I created a model with some internal state derived from a combination of
database and non-database data. Since I wanted to calculate these values
once rather than every time they’re requested by the view, I calculated
them in the initialize method and set some instance variables. However,
I soon noticed that these values weren’t getting set when I retrieved
data using the Model.find methods. After looking around the source
(specifically ActiveRecord::Base.instantiate), I see that Model.new is
never called. Rather, the allocate method is used and so the initialize
method I’ve defined is never called.

Why is it done this way? What would be the more appropriate way to
achieve the functionality I’m looking for?


#2

On 4/4/06, Jon B. removed_email_address@domain.invalid wrote:

Why is it done this way? What would be the more appropriate way to
achieve the functionality I’m looking for?

after_find or after_initialize may work for you. See
ActiveRecord::Callbacks.

-Isak