On 22 February 2011 19:42, ovinci <[email protected]> wrote:
> I tried to save and load a model and got the wrong value for a
> property if the property was set from a method
> The following test shows, what works and what doesn't
>
> I want to know, if I do something wrong and
> if there is a way to avoid the failure e.g. by setting a condition
>
> class SimpleChunk
>  include DataMapper::Resource
>
>  property :id,           Serial      # auto-increment integer key
>  property :content,      String
>  property :occured,      Integer,    :default => 0       # number of
> occurences
>
>  def occur(ntimes = 1)
>    @occured += ntimes
>  end
> end

Hi

The problem essentially boils down to your use of an instance
variable.  To avoid persisting everything each time, DataMapper does
what is called 'dirtiness tracking'.  This has to use methods, since
you can't hook behaviour based on instance variables being set (though
I think dkubb has some theories on how to do this).  So in the first
test example, as far as DM is concerned when it comes to saving the
model, the property hasn't been set, so the default is used instead.

You want to do something like:

def occurs(ntimes = 1)
self.occurred = self.occurred + ntimes
end

Or consider using dm-adjust, if that is appropriate.

Regards
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to