Question: I'm running into a `stack level too deep` and have an infinite loop, but I can't figure out why, so I'm hoping someone else can spot it.

I have this method in my Game model:

```
def self.record_win_or_tie(game_id)
  game = Game.find(game_id)

  if game.home_team_score > game.away_team_score 
    game.home_team_won = true
    game.save
  end

end

```

When I run it from the console for a game where the conditional is true (ie game.home\_team\_score is greater than game.away\_team\_score) if keeps running the same query over and over again.

```
SELECT `games`.* FROM `games` WHERE `games`.`id` = 1 LIMIT 1

```

If I run the code for a game\_id where the conditional is false, the query looking for the game only happens once and there is no infinite loop.

**\* UPDATE \***

I figured out that the problem was that I was calling the method from my GameObserver:

```
class GameObserver < ActiveRecord::Observer
  def after_save(game)
    Game.record_win_or_tie(game.id)
  end
end

```

However, I don't know how to adjust my code. The requirement is to automatically update either `game.home_team_won` or `game.away_team_won` after someone has updated `game.home_team_score` or `game.away_team_score`.

It seems like I can't use an observer to do this.

Answer: 
Could it be that you have defined an after\_save callback that calls Game.record\_win\_or\_tie again? That would explain the infinite recursion. 

Otherwise we'd need to see the entire Game model
If for some reason it has to be in an after\_save, instead of saving the current instance and triggering the after save, or adding spurious instance variables, call update on the db directly. 

```
if game.home_team_score > game.away_team_direct
  Game.update_all({:home_team_won => true}, :id => id)
end
# Check the syntax, as I wrote it off the top of my head

```

But personally, if possible I'd move it to a before\_save as mentioned in another answer.