Docunext


DataMapper Hook Loop Traps

December 7th, 2009

Long story made short: I can't definte a save hook with one class that involves another object getting saved, because it triggers the same hook again.

!! Unexpected error while processing request: stack level too deep

Doh!

Then I tried redefining the save method:

  def save
    myaccount = self.account
    myaccount.cached_journal_balance = myaccount.journal_balance
    self.myaccount.save
    super
  end

No go:

>> Listening on 0.0.0.0:3000, CTRL+C to stop
!! Unexpected error while processing request: wrong number of arguments (1 for 0)

Grrrr... I believe part of the problem is that the class I'm trying to work with is inherited by a couple others.

It looks like I might have been able to get away with this lame-o instance variable on a parent object:

  after :save, :account_balances
  before :save, :unsaved
  def unsaved
    entry = self.entry
    if entry.saved == nil
      entry.saved = 0
    end
  end
  def account_balances
    entry = self.entry
    if entry.saved == 0
      entry.saved = 1
      account = Account.get(account_id)
      puts account.id
      mybal = account.journal_balance
      puts mybal
      account.cached_journal_balance = mybal
      account.save
    end
  end

I'm not exactly sure why its working (and I don't think its working extremely well), but so far so good.

Related:

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022