Jun 24, 2009

DM hook before :save

I've been using DM's hooks a lot to do the customized validation. Today I was trying to set a property before save,
before :save, :set_order_no
def set_order_no
order_no = next_order_no if order_no.nil? || order_no.blank?
end
private
def next_order_no
...
end
view raw wont_work.rb hosted with ❤ by GitHub

It won't work.

Finally, I figured it out, use attribute_set
# dm modle
class SampleOrder
include DataMapper::Resource
property :id, Serial
property :order_no, String
...
before :save, :set_order_no
def set_order_no
attribute_set(:order_no, next_order_no) if order_no.nil? || order_no.blank?
end
private
def next_order_no
...
end
end

No comments: