From d03459746cd0bce83150270be0d150e2d42266be Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Tue, 8 Sep 2015 17:07:56 -0700 Subject: [PATCH] Fix missing method for nil associations --- app/models/game.rb | 2 +- app/models/play.rb | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/models/game.rb b/app/models/game.rb index 465823a3..4b0b6936 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -109,7 +109,7 @@ class Game < ActiveRecord::Base end def hand_to_beat - self.play_to_beat.play.hand + self.play_to_beat.try(:play).try(:hand) end def player_to_beat diff --git a/app/models/play.rb b/app/models/play.rb index 87fd5469..3b4463f7 100644 --- a/app/models/play.rb +++ b/app/models/play.rb @@ -24,16 +24,19 @@ class Play < ActiveRecord::Base # Validation methods def hand_valid? + # Gather the truths about the hand @truths = [] + # Validations + # Is there a hand_to_beat ? - if self.game.play_to_beat + if game.hand_to_beat # current player is not the player to beat - if (self.game.player_to_beat != self.player) + if (game.player_to_beat != player) # hand_type match? - @truths << (self.hand_type == self.game.play_to_beat.play.hand_type) + @truths << (self.hand_type == game.hand_to_beat.hand_type) if game.hand_to_beat.try(:hand_type) # beats? - @truths << (self.beats? self.game.play_to_beat.play) + @truths << (self.beats? self.game.hand_to_beat.try(:play)) end end @@ -49,13 +52,17 @@ class Play < ActiveRecord::Base if @truths.include? false errors.add(:player_cards, "invalid hand") end + # Did any validations return false? return (!@truths.include? false) end # Check if the value of this hand is higher than the hand_to_beat def beats?(play_to_beat) - @truth = self.high_card_value > play_to_beat.high_card_value - return @truth + if play_to_beat + self.high_card_value > play_to_beat.high_card_value + else + false + end end def high_card