0
class Package
  include DataMapper::Resource
  property :id,           Serial
  property :product,      String, :required => true 

  def self.create(attributes = {})
    puts 'I am in the Object method'
    #do something here with value of product before creating a new row
    create(attributes)
  end
end


p = Package.new
p.create :product=>'myprod'

我实际上想要一个围绕 Datamapper 提供的“创建”方法的包装器。这样在 Package 表中创建一行之前,我可以对“product”的值做一些事情。但是上面的实现是错误的,它似乎在循环调用中丢失了。我明白了

.......
.......
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
I am in the Object method
SystemStackError - stack level too deep:

我究竟做错了什么?我怎样才能实现我的目标

4

1 回答 1

4

您在代码中拥有的是递归定义。你必须避免这种情况。

class Package
  include DataMapper::Resource
  property :id,           Serial
  property :product,      String, :required => true 

  def self.create(attributes = {})
    puts 'I am in the Object method'
    #do something here with value of product before creating a new row
    super(attributes)
  end
end
于 2012-10-16T19:36:58.833 回答