Created
March 7, 2013 22:56
-
-
Save ryanchin/5112626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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