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

  • Home
  • SEARCH
  • 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 6835121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:11:40+00:00 2026-05-26T23:11:40+00:00

I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,

  • 0

I have this code in my user model:

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  attr_accessor :password

  before_save :encrypt_password

  validates :email, :presence => true,
                    :uniqueness => { :case_sensitive => false },
                    :format => { :with => /\A[^@]+@[^@]+\z/ },
                    :length => 7..128
  validates :password, :presence => true,
                       :confirmation => true,
                       :length => 6..128

  private
    def encrypt_password
      return unless password
      self.encrypted_password = BCrypt::Password.create(password)
    end
end

Now in my controller when I’m updating some user fields with

@user.update_attributes(params[:user])

the password field is always validated, even when it is not set in the params hash. I figured that this is happening because of the attr_accesor :password which always sets password = “” on update_attributes.

Now I could simply skip the validation of password if it is an empty string:

validates :password, :presence => true,
                     :confirmation => true,
                     :length => 6..128,
                     :if => "password.present?"

But this doesn’t work because it allows a user to set an empty password.

Using update_attribute on the field I’d like to change is not a solution because i need validation on that attribute.
If I pass in the exact parameter with

@user.update_attributes(params[:user][:fieldname])

it doesn’t solve the problem because it also triggers password validation.

Isn’t there a way to prevent attr_accesor :password from always setting password = “” on update?

  • 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-26T23:11:41+00:00Added an answer on May 26, 2026 at 11:11 pm

    New answer

    This works for me:

    validates :password, :presence     => true,
                         :confirmation => true,
                         :length       => { :minimum => 6 },
                         :if           => :password # only validate if password changed!
    

    If I remember correctly it also took me some time to get this right (a lot of trial and error). I never had the time to find out exactly why this works (in contrast to :if => "password.present?").

    Old answer – not really useful for your purpose (see comments)
    I get around this problem by using a completely different action for password update (user#update_password). Now it is sufficient to only validate the password field

    :on => [:create, :update_password]
    

    (and also only make it accessible to those actions).

    Here some more details:

    in your routes:

    resources :users do
      member do
        GET :edit_password # for the user#edit_password action
        PUT :update_password # for the user#update_passwor action
      end
    end
    

    in your UsersController:

    def edit_password
      # could be same content as #edit action, e.g.
      @user = User.find(params[:id])
    end
    def update_password
      # code to update password (and only password) here
    end
    

    In your edit_password view, you now have a form for only updating the password, very similar to your form in the edit view, but with :method => :put and :url => edit_password_user_path(@user)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code for my user model: class User < ActiveRecord::Base has_secure_password
I have a user model class User < ActiveRecord::Base has_many :messages_received, :class_name=>Message, :foreign_key=>'receiving_id', :conditions
Let's say I have two models like so: class Comment < ActiveRecord::Base belongs_to :user
I have a simple model class Ad < ActiveRecord::Base has_many :ad_items end class AdItem
I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have the following models: class Product < ActiveRecord::Base belongs_to :brand belongs_to :model accepts_nested_attributes_for
So I have two models here: class Screen < ActiveRecord::Base belongs_to :user validates :screen_size,
I have the models User and StoredItem: class UserData < ActiveRecord::Base has_many :stored_items, :dependent
I have this block of code: users = Array.new users << User.find(:all, :conditions =>
I have a greasemonkey user script with this single line of code... window.close(); but

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.