基本上我有這個用戶模型具有一定的屬性說「健康」和另一戰模型,記錄所有用戶之間的鬥爭。用戶可以互相爭鬥,有些概率會決定誰勝出。兩人在戰鬥後都會失去生命。這是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