0

我为数据库使用数据映射器。我有一张桌子。

class ZedTable
  include DataMapper::Resource
  property :id,         Serial
  property :label,       String 
  property :now,    Boolean, :default => false  

  before :save do 
    ZedTable.all.update(:now => false)
    self.now = true
  end
end

也就是说,我只想要一个值 was true。但是当我保存数据时出现错误。

Failure/Error: Unable to find matching line from backtrace
 SystemStackError:
   stack level too deep

为什么?我该如何解决这个问题?谢谢。

4

1 回答 1

0

你得到了,stack too deep因为当你调用时update,它首先before :save再次调用钩子。您需要的方法是update!,它绕过了钩子。

于 2012-07-28T18:40:18.620 回答