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 が提供する「create」メソッドのラッパーが必要です。そのため、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 に答える