Created
March 13, 2012 10:06
-
-
Save Sephi-Chan/2027958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
def buy_weapon(weapon_type) | |
User.transaction do | |
weapon = Weapon.create(weapon_type: weapon_type) | |
self.decrement!(:money, weapon_type.cost) | |
Log.create(message: "User #{id} bought a #{weapon_type.name} for #{weapon_type.cost}.") | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def buy_weapon | |
weapon_type = WeaponType.find(params[:weapon_type_id]) | |
current_user.buy_weapon(weapon_type) | |
redirect_to dashboard_url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment