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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:24:09+00:00 2026-05-20T12:24:09+00:00

I set up a User AR model which has conditional validation which is pretty

  • 0

I set up a User AR model which has conditional validation which is pretty much identical to the Railscast episode on conditional validation. So basically my User model looks like this:

class User < ActiveRecord::Base
  attr_accessor :password, :updating_password

   validates :password, :presence => true,
             :confirmation => true,
             :length => { :within => 6..40 },
             :if => :should_validate_password?

  def should_validate_password?
    updating_password || new_record?
  end
end

Now in my action where the User can change their password I have the following two lines:

@user.updating_password = true
if @user.update_attributes(params[:user]) ...

so that I flag the validations to be run on the password. In development mode this works great – if the user tries to put in a password that is too short or too long the model does not pass validation. My problem is that for the life of me I can not get my tests for this to pass. Here is my spec:

require 'spec_helper'

 describe PasswordsController do
   render_views

   before(:each) do
     @user = Factory(:user)
   end

   describe "PUT 'update'" do

     describe "validations" do

     before(:each) do
       test_sign_in(@user)
     end

       it "should reject short passwords" do
         short = "short"
         old_password = @user.password
         @attr2 = { :password => short, :password_confirmation => short }
         put :update, :user_id => @user, :old_password => @user.password, :user => @attr2
         @user.password.should == old_password
       end

       it "should reject long passwords" do
         long = "a" * 41
         old_password = @user.password
         @attr2 = { :password => long, :password_confirmation => long }
         put :update, :user_id => @user, :old_password => @user.password, :user => @attr2
         @user.password.should == old_password
       end
      end
     end
    end

When I run these tests I always get the error:

1) PasswordsController PUT 'update' validations should reject short passwords
 Failure/Error: @user.password.should == old_password2
   expected: "foobar"
        got: "short" (using ==)

and of course the error for the password being too long. But should’nt the password be validated as a result of me setting @user.updating_password = true before any save attempts in the controller?

  • 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-20T12:24:10+00:00Added an answer on May 20, 2026 at 12:24 pm

    I think the problem isn’t the code but what you expect it to do. When you call update_attributes and pass in a bad value, the value is saved into the model object even though the validation fails; the bad value has not been pushed to the database.

    I think this makes sense because when the validation fails normally you would show the form again with the error messages and the inputs populated with the bad values that were passed in. In a Rails app, those values usually come from the model object in question. If the bad values weren’t saved to the model they would be lost and your form would indicate that the old ‘good’ values had failed validation.

    Instead of performing this check:

    @user.password.should == old_password
    

    Maybe try:

    @user.errors[:password].should_not == nil
    

    or some other test that makes sense.

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

Sidebar

Related Questions

I'm trying to set up user-friendly captions in a DataSet which will be detected
Is there a way to set up a user group that has permissions to
How can I find the font that the user has set in their Windows
I have a model called Answer which has a ForeignKey relationship to another model
in a simple drawing application I have a model which has a NSMutableArray curvedPaths
I've got a User model that has many Items. A Rating belongs to a
I've a custom property on a user control which has multiple state/modes. If this
I'm using HttpListener to allow a user to set up a proxy on a
I have a requirement for an admin user to set up an export directory
We have set up a system where notifications get sent to a user with

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.