
#initialize does not get called upon loading from find
Reported by Bobby Calderwood | July 26th, 2008 @ 07:31 PM
class Model
include DataMapper::Resource
property :id, Integer, :serial => true
attr_accessor :bar
def initialize
@bar = "baz"
end
end
foo = Model.new
foo.bar #=> "baz"
foo.save
foo = Model.first
foo.bar #=> nil
Why is Model#initialize not called when the model is instantiated from a find? That was surprising to me.
Comments and changes to this ticket
-
Dan Kubb (dkubb) December 1st, 2008 @ 01:56 AM
- State changed from new to invalid
- Assigned user cleared.
When loading an object from a persisted state we use Model.allocate to get an instance, and then set the ivars in the object using the values that were retrieved.
It's by design that we bypass #initialize. Perhaps if a good case could be made, we'd consider using #initialize, although currently there are no plans to do so.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »