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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:35:46+00:00 2026-06-03T01:35:46+00:00

I’m currently trying my hand at developing a simple web based game using rails

  • 0

I’m currently trying my hand at developing a simple web based game using rails and Mongoid. I’ve ran into some concurrency issues that i’m not sure how to solve.

The issue is i’m not sure how to atomically do a check and take an action based upon it in Mongoid.

Here is a sample of the relevant parts of the controller code to give you an idea of what i’m trying to do:

battle = current_user.battle
battle.submitted = true
battle.save

if Battle.where(opponent: current_user._id, submitted: true, resolving: false).any?
  battle.update_attribute(:resolving, true)
  #Resolve turn

A battle is between two users, but i only want one of the threads to run the #Resolve turn. Now unless i’m completely off both threads could check the condition one after another, but before setting resolving to true, therefore both end up running the ‘#Resolve turn’ code.

I would much appreciate any ideas on how to solve this issue.

I am however getting an increasing feeling that doing user synchronization in this way is fairly impractical and that there’s a better way altogether. So suggestions for other techniques that could accomplish the same thing would be greatly appreciated!

  • 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-03T01:35:47+00:00Added an answer on June 3, 2026 at 1:35 am

    Sounds like you want the mongo findAndModify command which allows you to atomically retrieve and update a row.

    Unfortunately mongoid doesn’t appear to expose this part of the mongo api, so it looks like you’ll have to drop down to the driver level for this one bit:

    battle = Battle.collection.find_and_modify(query: {oppenent: current_user._id, ...},
                                               update: {'$set' => {resolving: true})
    

    By default the returned object does not include the modification made, but you can turn this on if you want (pass {:new => true})

    The value returned is a raw hash, if my memory is correct you can do Battle.instantiate(doc) to get a Battle object back.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.