Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Why is user.save true but email shows as nil?
- user = User.new
- user.email = ""
- user.password = ""
- user.profile = Profile.new
- user.profile.first_name = ""
- ...
- user.profile.save
- user.save
- class User < ActiveRecord::Base
- attr_accessor :password, :email
- has_one :profile, :dependent => :destroy
- accepts_nested_attributes_for :profile
- validates :email, :uniqueness => true,
- :length => { :within => 5..50 },
- :format => { :with => /^[^@][w.-]+@[w.-]+[.][a-z]{2,4}$/i }
- validates :password, :confirmation => true,
- :length => { :within 4..20 },
- :presence => true,
- :if => :password_required?
- before_save :encrypt_new_password
- after_save :create_profile
- def self.authenticate(email, password)
- user = find_by_email(email)
- return user if user && user.authenticated?(password)
- end
- def authenticated?(password)
- self.hashed_password == encrypt(password
- end
- protected
- def encrypt_new_password
- return if password.blank?
- self.hashed_password = encrypt(password)
- end
- def password_required?
- hashed_password.blank? || password.present?
- end
- def encrypt(string)
- Digest::SHA1.hexdigest(string)
- end
- end
Add Comment
Please, Sign In to add comment