If I define the following model…
class Foo
include DataMapper::Resource
property :name, String, :key => true
before :save, do
puts 'save'
end
before :update, do
puts 'update'
end
end
Why does the 2nd save also trigger the ‘update’ hook?
ruby :001 > f = Foo.new
=> #<Foo @name=nil>
ruby :002 > f.name = 'Bob'
=> "Bob"
ruby :003 > f.save
save
=> true
ruby :004 > f.name = 'Joe'
=> "Joe"
ruby :005 > f.save
save
update
=> true
Of course I could dive into the source and answer the question of what code drives this behavior. More importantly, I want to understand the right way to use each of these hooks in practice.
Which returns:
So as you can see you’d want to use
:savehooks whenever something should happen after either a create or an update, and:createand/or:updatewhen you want a finer level of control.