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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:31:17+00:00 2026-06-10T12:31:17+00:00

Short: How do you protect against race conditions on saving ActiveRecord objects in Ruby

  • 0

Short:

How do you protect against race conditions on saving ActiveRecord objects in Ruby on Rails? Or is there at least a way to get an error if a race was detected?

Long:

Assume we have this Model

class Game < ActiveRecord::Base
  belongs_to :current_user, :class_name => "User", 
                  :foreign_key => "current_user_id"
  belongs_to :other_user, :class_name => "User", 
                  :foreign_key => "other_user_id
end

When creating games in the controller I want find the first game where :other_user_id is nil, set it to the user trying to create the game and then save the updated game.If no such game is found, create a new game with the asking user as current_user.

I can’t find anything in the documentation on ActiveRecord that shows how to protect against a race on saving the updated game i.e. user b could come in find the empty game and save to the database after user a has found the game but before they’ve saved to the database. If anything, what I’ve read on uniqueness validations seems like I need to fix this at the database level.

Thanks in advance!

  • 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-10T12:31:18+00:00Added an answer on June 10, 2026 at 12:31 pm

    You essentially want optimistic locking. This is enabled by having the lock_version column in game. So in a migration add something along the lines of:

     class AddLockedVersionToGames < ActiveRecord::Migration
       def changes
         add_column :games, :lock_version, :integer, :default => 0
       end
     end
    

    And in the game model:

      def self.add_user_to_game(current_user)
        game = Game.find_by_other_user(nil)
        if game
          game.other_user = current_user
        else
          game = Game.new(current_user: current_user)
        end
        game.save
      rescue ActiveRecord::StaleObjectError
        game = Game.new(current_user: current_user)
        game.save
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this short function to protect against my_sql injection, because of its
Is there any keyboard short cut to compile selected file in C++ project in
I've mangaed to get a suite of Junit tests running against a blackberry project
To protect against password brute-forcing, and to protect against timing attacks trying to detect
I am currently on a short research project. The company I work at has
I'm currently working on a small short-lived project. But despite the size it's complicated
I'm trying to use linq to sql for my project (very short deadline), and
Short version: I have a Django project under development & testing (not yet into
Short and simple, our project has a Launcher component and an Application component and
I have a .Net winforms project with a short time table (about a week

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.