Forum: Ruby on Rails Why no call to Model.new on find?

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.
F0a898f6a6131fe68d79ba1a6c060dd9?d=identicon&s=25 Jon Bauman (baumanj)
on 2006-04-04 00:17
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?
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2006-04-04 01:15
(Received via mailing list)
On 4/4/06, Jon Bauman <jonrubyforum@shumi.org> 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
This topic is locked and can not be replied to.