# After\_save woes

**URL:** <https://www.ruby-forum.com/t/after-save-woes/154088>\
**Category:** Rails\
**Created:** [November 25, 2008, 9:30am UTC](https://www.ruby-forum.com/t/after-save-woes/154088 "2008-11-25T09:30:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@ryank](https://www.ruby-forum.com/u/ryank)\
**Post date:** [November 25, 2008, 9:30am UTC](https://www.ruby-forum.com/t/after-save-woes/154088/1 "2008-11-25T09:30:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ryank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@ryank](https://www.ruby-forum.com/u/ryank)\
**Post date:** [November 25, 2008, 9:32am UTC](https://www.ruby-forum.com/t/after-save-woes/154088/2 "2008-11-25T09:32:57Z")

</div>

On Nov 25, 12:32 am, Ryan K. [removed\_email\_address@domain.invalid](mailto: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
