After_save woes


#1

Two models, a game, and a player. The game should start as soon as
there are enough players in the game
#####################################################

class Game < ActiveRecord::Base
has_many :players

should_start
return players.count >= number_of_players #number_of_players is
defined in the database
end

def start
started = true #started is a variable defined in the database
save
end
end

class Player < ActiveRecord::Base
belongs_to :game
after_save :check_game_start

def check_game_start
game.start if game.should_start?
end
end

#########################################
The problem with this code, is that the record has not yet been saved
to the database by the time after_save has been called. Looking
around the forum, it appears that there is no after_commit callback.
What is the correct way to do this? Should I change the game to start
at some other point, should I change the RoR code to add the callback
myself, or should I add some hack to make it guess what the new state
of the database is to get it to start?


#2

On Nov 25, 12:32 am, Ryan K. removed_email_address@domain.invalid wrote:

end

def start
started = true #started is a variable defined in the database
save
not the problem here but just as a FYI your start method is a no-op.
you’re setting a local variable, not the database attribute.

Fred