Skip to content

Instantly share code, notes, and snippets.

@motdotla
Forked from ewilliam/gist:5171037
Last active December 15, 2015 01:49
Show Gist options
  • Save motdotla/5183011 to your computer and use it in GitHub Desktop.
Save motdotla/5183011 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :lvl, :xp, :hp, :ap, :max_xp, :max_hp, :max_ap, :type
has_secure_password
before_save { email.downcase! }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
# validates :password, presence: true, length: { minimum: 6 }
# validates :password_confirmation, presence: true
after_initialize :set_any_nil_values_to_defaults
private
def set_any_nil_values_to_defaults
self.xp = 0 unless xp
self.max_xp = 50
self.hp = 4
self.max_hp = 4
self.ap = 4
self.max_ap = 4
self.lvl = 1
end
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
@motdotla
Copy link
Author

User.new

User.new(xp: 100)

User.create(xp: 100)

User.find(id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment