Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created March 13, 2012 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sephi-Chan/2027958 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/2027958 to your computer and use it in GitHub Desktop.
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
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