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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:05:23+00:00 2026-05-20T22:05:23+00:00

I need to perform some atomic arithmetic in Rails but the only way I’ve

  • 0

I need to perform some atomic arithmetic in Rails but the only way I’ve found to do it for single objects is via the crude update_all class method, e.g.:

Account.update_all(["debits = debits + ?", amount], :id => id)

With collection associations, the update_all class method should be usable as an association method, since the collection will pass missing method calls on to the class with the relevant scope:

accounts.update_all(["debits = debits + ?", amount])

When dealing with collections, this is much nicer and less repetitive. However this doesn’t work for singleton associations, i.e. belongs_to and has_one. The method_missing for AssociationProxy passes through to the target instance, which won’t have an update_all instance method (naturally).

Is there a more elegant way to perform this arithmetic? Or is update_all as good as it gets?

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

    I think what you are looking for is the update_counters method. It allows you to specify an arbitrary increment value and still performs an atomic operation:

    Account.update_counters id, :debits => amount
    

    It also supports performing the operation on collections of ids without a loop:

    Account.update_counters [1, 2, 3, 4, 5], :debits => amount
    

    Check out the most recent Rails 3.0.x documentation:

    http://api.rubyonrails.org/classes/ActiveRecord/CounterCache.html#method-i-update_counters

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

Sidebar

Related Questions

I need to perform some action in wordpress admin panel programmatically but can't manage
Suppose you need to perform some kind of comparison amongst 2 files. You only
I need to perform some action when my application receives focus. I've tried hooking
I need to perform some basic OLS regression using F#. To do this I
I need to perform some date operations in ASP.net using C#. The date i
I need to perform some actions when list box selection is about to changed,
I need to perform some operations on all folders within a file share which
I need to perform some file operations on WinCE platform. The paths of file
i have a input tag which is non editable, but some times i need
I need to perform some string manipulation to a columns contents in a GridView,

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.