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 8869579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:30:42+00:00 2026-06-14T17:30:42+00:00

I am somewhat new to Rails and working on designing a User model using

  • 0

I am somewhat new to Rails and working on designing a User model using ActiveRecord. In this model, I have a password attribute which is intended to keep a hash of the user’s password.

I want to remove both reading and setting of this attribute directly. However, I can’t seem to find a way to remove the accessors when using the Rails console. The only viable solution so far has been to explicitly override the accessor methods for password and I don’t really want to override them, I want the accessors gone – or at least the reader.

Here is my model:

class User < ActiveRecord::Base

  // various associations

  def password_correct?(password)
    read_attribute(:password) == hash(password)
  end

  def password=(password)
    write_attribute(:password, hash(password))
  end

  def password
    "get your dirty fingers off this attribute"
  end

  private

  def hash(input)
    Digest::SHA2.new(512).update(input).hexdigest
  end

end

Any ideas how to achieve this or any shortcomings to this approach?

  • 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-06-14T17:30:44+00:00Added an answer on June 14, 2026 at 5:30 pm

    Based on the above answers, I have done some experimentation to obtain the desired result. I ended up making a “private” password_hash attribute and a virtual accessor called password.

    I made some observations in this process:

    • It seems that ActiveRecord doesn’t have any concept of private attributes. Making the accessor methods private using symbols such as private :password, :password= is not an option, because Rails throws an NameError: undefined method when instantiating a model, as the model itself does not have these two methods defined (they seem to be inherited from ActiveRecord::Base).

    • Overriding the password_hash accessors with pure nothing is great for preventing the manipulation of the attribute, however it also means that ActiveRecord itself fails when updating the password_hash attribute as it is calling an empty implementation.

    So making the accessors private fails because they’re undefined in the actual model. Defining them also fails, because it breaks ActiveRecord. So what can you do?

    I did both and a bit more. I made the accessors private, defined them and implemented both by calling super. This prevents the controller (and the rails console) from accessing them by throwing a NoMethodError, but doesn’t reject ActiveRecord.

    A side note: Validation issues

    One problem I encountered with my approach was broken validation. Enforcing a minimum length on the password_hash was no good as any password (even nothing) results in a 128 character SHA512 hash. So validating the hash made little sense. Instead I added validation to the virtual password accessor and added a before_save :hash_password callback which checks to see if the virtual accessor attribute has been set and if so, hashes it and writes it to the password_hash attribute.

    Final implementation

    My implementation ended up this way:

    class User < ActiveRecord::Base
      attr_accessible :first_name, :last_name, :email
      attr_accessor :password
      validates :password, :length => { :minimum => 8 }, :if => :password_changed?
      validates :first_name, :last_name, :email, presence: true
      # Various associations
      before_save :hash_password
    
      def password_correct?(p)
        if(password.present?)
          password == p
        else
          read_attribute(:password_hash) == hash_string(p)
        end
      end
    
      def role_symbols
        roles.collect do |r|
          r.name.to_sym
        end
      end
    
      private
    
      def hash_string(input)
        Digest::SHA2.new(512).update(input).hexdigest
      end
    
      def hash_password
        if(password.present?)
          write_attribute(:password_hash, hash_string(password))
          self.password = nil
        end
      end
    
      def password_changed?
        password.present? or new_record?
      end
    
      def password_hash
        super
      end
    
      def password_hash=(p)
        super
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am somewhat new to R, and i have this piece of code which
I'm somewhat new to Rails and have been working with the scopes in Rails
I'm sure this is pretty basic, but I'm somewhat new to rails and struggling
I am somewhat new to Jruby - up to this point I have had
Somewhat new to android, need some help with services. I have a service which
I am somewhat new to android development and development in general. I have a
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
So I am somewhat new to Rails 3, and I am trying to integrate
A quick warning: I am pretty new to Rails, and my knowledge is somewhat
I'm somewhat new to rails and every time I think I'm getting it I

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.