views:

51

answers:

1

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
Thanks, I don't want to do it every time it is saved though
Jeffrey Aylesworth
@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