Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 758595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:29:52+00:00 2026-05-14T15:29:52+00:00

This code is from the agile web development with rails book.. I don’t understand

  • 0

This code is from the agile web development with rails book.. I don’t understand this part of the code…
User is a model which has name,hashed_password,salt as its fields. But in the code they are mentioning about password and password confirmation, while there are no such fields in the model. Model has only hashed_password. I am sure mistake is with me. Please clear this for me 🙂
User Model has name,hashed_password,salt. All the fields are strings

require 'digest/sha1'
class User < ActiveRecord::Base 
      validates_presence_of :name
      validates_uniqueness_of   :name
      attr_accessor :password_confirmation 
      validates_confirmation_of :password
      validate :password_non_blank

      def self.authenticate(name, password) 
          user = self.find_by_name(name) 
          if user
             expected_password = encrypted_password(password, user.salt)             
             if user.hashed_password != expected_password
                user = nil 
             end
          end
          user
      end

      def password 
          @password
      end

      def password=(pwd) 
          @password = pwd 
          return if pwd.blank? 
          create_new_salt 
          self.hashed_password = User.encrypted_password(self.password, self.salt)
      end

      private
        def password_non_blank 
            errors.add(:password,"Missing password")if hashed_password.blank?
        end

        def create_new_salt 
            self.salt = self.object_id.to_s + rand.to_s
        end

        def self.encrypted_password(password, salt) 
            string_to_hash = password + "wibble" + salt  
            Digest::SHA1.hexdigest(string_to_hash)
        end 
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:29:52+00:00Added an answer on May 14, 2026 at 3:29 pm

    attr_accessor is used to create getter/setter methods for an instance variable. For example:

    attr_accessor :foo
    
    # is equivalent to
    
    def foo
      @foo
    end
    
    def foo=(value)
      @foo = value
    end
    

    In the code you pasted, def password and def password= are defined manually. However, I’m a bit confused by the use of:

    attr_accessor :password_confirmation
    

    validates_confirmation_of :foo automatically creates a foo_confirmation accessor, so this should be:

    attr_accessor :password
    validates_confirmation_of :password
    

    Add a simple before_save callback to encrypt the password and you’re all done.

    require 'digest/sha1'
    class User < ActiveRecord::Base
    
      # attrs
      attr_accessor :password
    
      # class methods
      class << self
        def encrypt(password, salt)
          Digest::SHA1.hexdigest("--#{salt}--#{password}--");
        end
      end
    
      # validation
      validates_presence_of       :name
      validates_confirmation_of   :password
    
      # callbacks
      before_save :encrypt_password
    
      protected
    
      def encrypt_password
        return if password.blank?
        if new_record?
          self.salt = Digest::SHA1.hexdigest("--#{Time.now}--#{name}--")
        end
        self.encrypted_password = User.encrypt(password, salt)
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Taking this snippet from the famous agile web development with rails book (most recent
I'm following through the tutorial from the book Agile Web Development with Rails and
I am working through the Agile Web Development with Rails book but I have
Hey. I've got some code from Agile Web Development that wraps some HTML around
I am learning Rails from Agile Web Development With Rails (4th edition, Rails 3)
OK, so I am reading Agile Web Development with Rails and working on the
I'm reading the book Agile web developement with Rails 4Th editon, when he present
I'm following the Agile web application development with yii 1.1 and php5 book and
I am working through Agile Web Development with Rails 4th Edition (Rails 3.2+) and
Does Agile Web Development With Ruby on Rails (Third Edition) teach best practices as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.