2012-07-28 61 views
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 

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

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

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

回答

0

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