Ruby on Rails: Why does the user's encrypted password get changed in the DB when I use the "toggle" method on the admin attribute? -


I have just finished the Hartl Rail Tutorials book and I am using it in my first Rail app. However, when I create a new user account and set it as an administrator account by toggling the administrator attribute in the console (eg User.find (5) .toggle (: admin)), the encrypted password that is stored in DB is modified Why does it go?

User model logic here ...

  class users & lt; ActiveRecord :: Base #virtual attr_accessor properties: Password #attrs attr_accessible outside world amendment His name ,: Email ,: Password: password_confirmation email_regex = /\A[\w+\-.]+@[az\d\-. ] + \. [Az] + \ z / i #validations Valid: Name ,: appearance = & gt; True,: length = & gt; {: Max = & gt; 50} Valid: Email ,: Attendance = & gt; True: format = & gt; {: With = & gt; Email_regex} ,: Specification = & gt; Correct Confirmation: Password ,: Attendance = & gt; True, Confirmation = & gt; True,: length = & gt; {: Inner = & gt; 6..40} # class method that authenticates a user, a session cookie is used to make DEF self.authenticate (email, submit_password) user = find_by_email (email) returns blue if user.nil? User return if user.has_password? (Submitted_password) end (ID, Cookie_salt) user = find_by_id (ID) return nil, so user.nil #used to authenticate a user signed a signed cookie def self.authenticate_with_salt? Return user user.salt is == occurs cookie_salt end is a record before #callback saved successfully (meaning that it is a valud password) if before_save: encrypt_password def has_password (submitted_password) encrypted_password encrypt == (Submitted_password) end Personal #self is the keyword when new_record is required when specifying a frequency att def encrypted_password self.salt = make_salt? self.encrypted_password = Encrypt (password) end def encrypt (string) Secure_hash ( "# {salt} - # {string}") end def Make_salt Secure_hash ( "" # {Timeknowkutc} - # {password}) end Def secure_hash (string) digest :: SHA2.hexdigest (string) end-end   

and here's the way to behave ... looks

  Manik-1.9 .2-P 180: 018 & gt; User.last = & gt; # & Lt; User ID: 12, Name: "Test user A", email: "testusera@website.com", created_t: "2011-03-28 17:47:42", updated_at: "2011-03-28 17: 47: 42 "salt:" 23ca99e9848336a078e05ce9a8d904f9dfdff30dc7a38586f22 ... ", admin wrong, Masik_sor zero, Ankripted_pasward:" 50d17e6d6b581cfcfe84b61feb318705978cdf6c435626d10aa ... "& gt; Ruby-1.9.2-P 180: 01 9 & gt; User.last.toggle! (: Admin) = & gt; True ruby ​​- 1.9.2-P 180: 020 & gt; User.last = & gt; # & Lt; User ID: 12, Name: "Test User A", Email: "testusera@website.com", created_t: "2011-03-28 17:47:42", updated_at: "2011-03-28 17: 49: 06 "salt:" 23ca99e9848336a078e05ce9a8d904f9dfdff30dc7a38586f22 ... ", administrator: right, Masik_sor zero, Ankripted_pasward:" 5d6e17f7aa73925a0099DA45807f5994fa8c368a5a12d187a7d ... "& gt;   

Thanks a lot for your help!

Try your first_service method:

  def encrypt_password if Password.present? Self.salt = make_salt if new_record? Self.encrypted_password = Encrypt (password) end and   

UPD . Or you can minimize it

  def encrypted_password self.salt = make_salt if new_record? Self.encrypted_password = encrypt (password) password.present if? End    

Comments