How can I have a block execute whenever an instance of a DataMapper class is a created, and another before it is destroyed?
+3
A:
Assuming by create & destroy you mean insert and remove from the db...
From the documentation
class Post
include DataMapper::Resource
# ... key and properties here
# This record will save properly
before :create do |post|
true
end
# But it will not be destroyed
before :destroy do |post|
throw :halt
end
end
BaroqueBobcat
2010-02-08 04:17:50
Thanks, I don't want to do it every time it is saved though
Jeffrey Aylesworth
2010-02-08 04:21:29
@Jeffrey, The before_create hook is only called when a record is first saved, not when it is updated. So just replace "before :save" with "before :create" in @BarqueBobcat's example.
Wayne Conrad
2010-02-08 04:40:36