Skip to content

Instantly share code, notes, and snippets.

@andrewhl
Created April 5, 2012 20:01
Show Gist options
  • Save andrewhl/2313665 to your computer and use it in GitHub Desktop.
Save andrewhl/2313665 to your computer and use it in GitHub Desktop.
1.9.2p290 :002 > user = User.find_by_email("t@test.com")
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = 't@test.com' LIMIT 1
=> #<User id: 101, email: "t@test.com", first_name: "t", last_name: "t", mother_first_name: "t", mother_last_name: "t", mother_email: nil, mother_work_phone: nil, mother_home_phone: "t", father_first_name: "t", father_last_name: "t", father_email: "t", father_work_phone: "t", father_home_phone: nil, address: "t", city: "t", postal_code: "t", username: nil, campus: nil, remember_token: "y_In_Q-ZX43Mg45R8x-VGA", password_digest: "$2a$10$zooQ6ZXJ12L36DI2vIFobe9wfviwEOwmfj9R9T1cg8wx...", teacher: false, admin: false, created_at: "2012-03-25 04:10:37", updated_at: "2012-03-25 04:10:37">
1.9.2p290 :003 > user
=> #<User id: 101, email: "t@test.com", first_name: "t", last_name: "t", mother_first_name: "t", mother_last_name: "t", mother_email: nil, mother_work_phone: nil, mother_home_phone: "t", father_first_name: "t", father_last_name: "t", father_email: "t", father_work_phone: "t", father_home_phone: nil, address: "t", city: "t", postal_code: "t", username: nil, campus: nil, remember_token: "y_In_Q-ZX43Mg45R8x-VGA", password_digest: "$2a$10$zooQ6ZXJ12L36DI2vIFobe9wfviwEOwmfj9R9T1cg8wx...", teacher: false, admin: false, created_at: "2012-03-25 04:10:37", updated_at: "2012-03-25 04:10:37">
1.9.2p290 :004 > user.admin = true
=> true
1.9.2p290 :005 > user
=> #<User id: 101, email: "t@test.com", first_name: "t", last_name: "t", mother_first_name: "t", mother_last_name: "t", mother_email: nil, mother_work_phone: nil, mother_home_phone: "t", father_first_name: "t", father_last_name: "t", father_email: "t", father_work_phone: "t", father_home_phone: nil, address: "t", city: "t", postal_code: "t", username: nil, campus: nil, remember_token: "y_In_Q-ZX43Mg45R8x-VGA", password_digest: "$2a$10$zooQ6ZXJ12L36DI2vIFobe9wfviwEOwmfj9R9T1cg8wx...", teacher: false, admin: true, created_at: "2012-03-25 04:10:37", updated_at: "2012-03-25 04:10:37">
1.9.2p290 :006 > user.save
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('t@test.com') AND "users"."id" != 101) LIMIT 1
(0.1ms) rollback transaction
=> false
1.9.2p290 :007 > User.find_by_email("t@test.com")
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."email" = 't@test.com' LIMIT 1
=> #<User id: 101, email: "t@test.com", first_name: "t", last_name: "t", mother_first_name: "t", mother_last_name: "t", mother_email: nil, mother_work_phone: nil, mother_home_phone: "t", father_first_name: "t", father_last_name: "t", father_email: "t", father_work_phone: "t", father_home_phone: nil, address: "t", city: "t", postal_code: "t", username: nil, campus: nil, remember_token: "y_In_Q-ZX43Mg45R8x-VGA", password_digest: "$2a$10$zooQ6ZXJ12L36DI2vIFobe9wfviwEOwmfj9R9T1cg8wx...", teacher: false, admin: false, created_at: "2012-03-25 04:10:37", updated_at: "2012-03-25 04:10:37">
class User < ActiveRecord::Base
attr_accessible :first_name,
:last_name,
:email,
:mother_first_name,
:mother_last_name,
:mother_work_phone,
:mother_home_phone,
:father_first_name,
:father_last_name,
:father_email,
:father_work_phone,
:address,
:city,
:postal_code,
:username,
:password,
:password_confirmation
has_secure_password
has_many :children
before_save :create_remember_token
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_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, length: { minimum: 6 }
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment