我可以覆盖 DataMapper 的保存、删除、销毁!使用模块的方法,例如:
require 'data_mapper'
module Record
def self.included(base)
base.class_eval do
include DataMapper::Resource
property :id, DataMapper::Property::Serial
alias :parent_save :save
def save bar
# do additional thing with bar
end
end
end
end
class User
include Record
property :name,String
end
DataMapper.finalize
# so i could call something like this:
x = User.new
x.name = 'something'
x.save 123
如何覆盖create
和first_or_create
方法,当这些方法找不到时base.class_eval do
?
所以我可以这样称呼:
User.first_or_create additional_param, name: 'something'