Skip to content

Instantly share code, notes, and snippets.

@workmad3
Created February 10, 2010 17:31
Show Gist options
  • Save workmad3/300591 to your computer and use it in GitHub Desktop.
Save workmad3/300591 to your computer and use it in GitHub Desktop.
class Request < ActiveRecord::Base
before_save :reduce_users_karma
validate :validate_karma
def validate_karma
if user
errors.add(:karma, "You don't have enough karma") unless user.karma_current > karma
else
errors.add(:user, "No user in request")
end
end
def reduce_users_karma
user.karma -= karma
user.save
end
end
def create
@req = Request.new(params[:request])
if @req.save
flash[:notice] = "Request created!"
redirect_to requests_path
else
flash[:error] = "We couldn't create your request, sorry."
render :action => 'new'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment