基本上我有这个用户模型具有一定的属性说“健康”和另一战模型,记录所有用户之间的斗争。用户可以互相争斗,有些概率会决定谁胜出。两人在战斗后都会失去生命。这是Rails 3中的竞态条件问题吗?
所以在战役控制器, '创建' 动作我做了,
@battle = Battle.attempt current_user.id, opponent.id
在战模型,
def self.attempt current_user.id, opponent_id
battle = Battle.new({:user_id => current_user.id, :opponent_id => opponent_id})
# all the math calculation here
...
# Update Health
...
battle.User.health = new_health
battle.User.save
battle.save
return battle
end
回到战控制器,我做...
new_user_health = current_user.health
在战斗结束后获得新的健康价值。然而,我得到的价值是旧的健康价值(战前的健康价值)。
有没有人在之前遇到过这种问题?
UPDATE
我行
new_user_health = current_user.health
和工作前加
current_user.reload
。问题解决了。谢谢!
我_think_这是你正在面对的:http://stackoverflow.com/questions/5519741/how-can-i-know-when-to-refresh-my-model-object-in-rails – sarnold 2012-01-18 02:28:41
哪部分令人困惑?雅这是实际的代码,但我只突出主要的东西 – 2012-01-18 02:28:50
我编辑代码升技,现在不那么混乱? – 2012-01-18 02:30:46