I have a simplified model that looks like this:
class Player < ActiveRecord::Base
has_many :picks
def pick_for_game(game)
id = game.instance_of?(Game) ? game.id : game
pick = picks.find {|pick| pick.game_id == id}
if !pick
pick = picks.build(:game_id => id)
end
pick
end
end
The pick_for_game
method usually is called a bunch of times when an action is executed. What would be a good way to make this code work efficiently with a list of games?