
DataMapper::Resource is not calling initialize definined in models.
Reported by Postmodern | September 16th, 2009 @ 04:43 PM
I just noticed that DataMapper 0.10.0 is not calling the "initialize" method defined in the models. I'm not sure when this behavior regressed.
require 'dm-core'
class User
include DataMapper::Resource
property :id, Serial
property :name, String
attr_reader :var
def initialize(attributes={},&block)
super(attributes,&block)
@var = 2
end
end
DataMapper.setup(:default, 'sqlite3::memory:')
User.auto_migrate!
User.create(:name => 'joe')
puts User.first.inspect
puts User.first.var
As you can see from the example, resources returned by "first" do not have the instance variable @var set.
Comments and changes to this ticket
-
Postmodern September 16th, 2009 @ 05:03 PM
- Assigned user set to Dan Kubb (dkubb)
-
Dan Kubb (dkubb) September 16th, 2009 @ 05:26 PM
- State changed from new to confirmed
- Tag changed from 0.10.0, dm-core, first, initialization, regression, resource to 0.10.0, dm-core, first, initialization, resource
The initialize method is only called when originally creating the object, not when it is being loaded from the datastore.
While it could be argued that this behavior is wrong, I know it was originally chosen for performance reasons.
I'll think about this, and figure out if we can use the initialize method when loading or not.
The only problem I can think of is when you have a totally custom initialize method that takes something different from a single Hash.. if we call it on load, DM isn't going to know what arguments to pass in.
-
Postmodern September 16th, 2009 @ 06:58 PM
As an easy work around, you can create a custom Model module for your models to override Class#allocate.
module Project module Model def self.included(base) base.module_eval do def self.allocate resource = super resource.instance_eval("instance_eval()") return resource end end end end end
-
-
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 »