title: attr_accessor and password validation on update
body: <p>I have this code in my user model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :email, :password, :password_confirmation
    attr_accessor :password
  
    before_save :encrypt_password
  
    validates :email, :presence =&gt; true,
                      :uniqueness =&gt; { :case_sensitive =&gt; false },
                      :format =&gt; { :with =&gt; /\A[^@]+@[^@]+\z/ },
                      :length =&gt; 7..128
    validates :password, :presence =&gt; true,
                         :confirmation =&gt; true,
                         :length =&gt; 6..128
  
    private
      def encrypt_password
        return unless password
        self.encrypted_password = BCrypt::Password.create(password)
      end
  end
  </code></pre>
  
  <p>Now in my controller when I'm updating some user fields with</p>
  
  <pre><code>@user.update_attributes(params[:user])
  </code></pre>
  
  <p>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.</p>
  
  <p>Now I could simply skip the validation of password if it is an empty string:</p>
  
  <pre><code>validates :password, :presence =&gt; true,
                       :confirmation =&gt; true,
                       :length =&gt; 6..128,
                       :if =&gt; "password.present?"
  </code></pre>
  
  <p>But this doesn't work because it allows a user to set an empty password.</p>
  
  <p>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</p>
  
  <pre><code>@user.update_attributes(params[:user][:fieldname])
  </code></pre>
  
  <p>it doesn't solve the problem because it also triggers password validation.</p>
  
  <p>Isn't there a way to prevent attr_accesor :password from always setting password = "" on update?</p>