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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:27:13+00:00 2026-05-28T00:27:13+00:00

In section 6.2.4 of Ruby on Rails 3 Tutorial , Michael Hartl describes a

  • 0

In section 6.2.4 of Ruby on Rails 3 Tutorial, Michael Hartl describes a caveat about checking uniqueness for email addresses: If two identical requests come close in time, request A can pass validation, then B pass validation, then A get saved, then B get saved, and you get two records with the same value. Each was valid at the time it was checked.

My question is not about the solution (put a unique constraint on the database so B’s save won’t work). It’s about writing a test to prove the solution works. I tried writing my own, but whatever I came up with only turned out to mimic the regular, simple uniqueness tests.

Being completely new to rspec, my naive approach was to just write the scenario:

it 'should reject duplicate email addresses with caveat' do
  A = User.new( @attr ) 
  A.should be_valid          # always valid

  B = User.new( @attr )
  B.should be_valid          # always valid, as expected

  A.save.should == true      # save always works fine

  B.save.should == false     # this is the problem case
  # B.should_not be_valid    # ...same results as "save.should"
end

but this test passes/fails in exactly the same cases as the regular uniqueness test; the B.save.should == false passes when my code is written so that the regular uniqueness test passes and fails when the regular test fails.

So my question is “how can I write an rspec test that will verify I’m solving this problem?” If the answer turns out to be “it’s complicated”, is there a different Rails testing framework I should look at?

  • 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-28T00:27:14+00:00Added an answer on May 28, 2026 at 12:27 am

    It’s complicated. Race conditions are so nasty precisely because they are so difficult to reproduce. Internally, save goes something like this:

    1. Validate.
    2. Write to database.

    So, to reproduce the timing problem, you’d need to arrange the two save calls to overlap like this (pseudo-Rails):

    a.validate    # first half of a.save
    b.validate    # first half of b.save
    a.write_to_db # second half of a.save
    b.write_to_db # second half of b.save
    

    but you can’t open up the save method and fiddle with its internals quite so easily.

    But (and this is a big but), you can skip the validations entirely:

    Note that save also has the ability to skip validations if passed :validate => false as argument. This technique should be used with caution.

    So if you use

    b.save(:validate => false)
    

    you should get just the “write to database” half of b‘s save and send your data to the database without validation. That should trigger a constraint violation in the database and I’m pretty sure that will raise an ActiveRecord::StatementInvalid exception so I think you’ll need to look for an exception rather than just a false return from save:

    b.save(:validate => false).should raise_exception(ActiveRecord::StatementInvalid)
    

    You can tighten that up to look for the specific exception message as well. I don’t have anything handy to test this test with so try it out in the Rails console and adjust your spec appropriately.

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

Sidebar

Related Questions

I'm going through Michael Hartl's Ruby on Rails 3.2 Tutorial and I'm confused about
enter code hereI'm trying to finish up Michael Hartl's Ruby on Rails v3.2 Tutorial
I'm following the Rails Tutorial by Michael Hartl and I'm getting an unexpected error/failed
Came across this error when trying out the ruby on rails tutorial section with
I'm trying to learn both Ruby and Rails and I'm looking at Michael Hartl's
I'm running through Michael Hartl's Rails Tutorial . I'm trying to verify the title
Firstly, I'm not using rails. This is vanilla ruby application. I've read about packaging
I just finished chapter 10 of the Ruby on Rails Tutorial , adding the
I am running Rails 3.0.1 and Rspec 2.0.1. I am following M. Hartl's Ruby
I am having a ruby on rails application.I added a new section called reports

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.