Reputation: 3913
Here's the model:
class Target
include DataMapper::Resource
property :id, Serial
property :owed, Integer, :default => 0, :required => true
property :served, Integer, :default => 0, :required => true
def go
@owed -= 1
@served += 1
save
end
end
When I do this:
t = Target.first
t.go
It doesn't seem to update the database. Any ideas?
Upvotes: 1
Views: 501
Reputation: 454
The resource isn't saving because DataMapper doesn't know that the owed
and served
properties have been changed. #save
will only save the resource, if the resource is considered dirty by DataMapper.
Instead, change the values of self.owed
and self.served
, which will cause a state change within the resource, which will mark the resource as dirty and allow #save
to trigger. Additionally, you can simplify the go
method using #update
:
def go
update(:owed => self.owed - 1, :served => self.served + 1)
end
Upvotes: 4
Reputation: 124419
You need to use self.owed
and self.served
instead:
def go
self.owed -= 1
self.served += 1
save
end
Upvotes: 3