0

I use datamapper for the database. I have a table.

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

That is, I want only one value was true. But when I save the data I get an error.

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

Why? And how do I solve this problem? Thanks.

Mike
  • 1,042
  • 1
  • 8
  • 14

1 Answers1

0

You get are getting stack too deep because when you call update, it first calls before :save hook again. The method you need is update!, it bypasses the hooks.

ujifgc
  • 2,215
  • 2
  • 19
  • 21