Skip to content

Instantly share code, notes, and snippets.

@ryanchin
Created March 7, 2013 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanchin/5112626 to your computer and use it in GitHub Desktop.
Save ryanchin/5112626 to your computer and use it in GitHub Desktop.
# using after_save (this works)
class GameObserver < ActiveRecord::Observer
observe :game
def after_save(game)
game.record_win_or_tie
end
end
class Game < ActiveRecord::Base
def record_win_or_tie
return if @inside_callback
@inside_callback = true
if home_team_score > away_team_score
update_attributes(:home_team_won => true)
end
end
end
# before_save doesn't save the home_team_won
class GameObserver < ActiveRecord::Observer
observe :game
def before_save(game)
game.record_win_or_tie
end
end
class Game < ActiveRecord::Base
def record_win_or_tie
home_team_won = true if home_team_score > away_team_score
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment