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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:18:37+00:00 2026-06-13T08:18:37+00:00

I have a table with workouts with the following key columns: workout_id        distance

  • 0

I have a table with workouts with the following key columns:

workout_id      distance
1                       2.3
2                       3.1

Let’s say I want to write a Rails active record query so that I get these columns plus a running total of the distance, like so:

workout_id      distance      running_total
1                       2.3                 2.3
2                       3.1                 5.4

How could I do such a thing?

  • 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-13T08:18:38+00:00Added an answer on June 13, 2026 at 8:18 am

    I agree with HungryCoder that you should perhaps do it in the model rather than in a database query.

    class Workout
      # ...
      attr_accessor :running_total
      class << self
        def distance_with_running_total
          total = 0.0
          Workout.order(:id).collect do |w|
            total += w.distance
            w.running_total = total
          end
        end
      end
      # ...
    end
    

    Then in the controller, simply do:

    @workouts = Workout.distance_with_running_total
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have Table A with columns title and description , and I want
i am tracking exercises. i have a workout table with id exercise_id (foreign key
I have a grouped table view where I want to reload a particular row.
I have table: test and field called: id. I want update time (ex. add
I have table Players with columns: ID Name Points What is most efficient way
I have table the following data structure in SQL Server: ID Date Allocation 1,
I have table with data 1/1 to 1/20 in one column. I want the
I have built a ruby on rails app that lets users track their workouts.
I have table, and I want check/uncheckcheck box not only by clicking on checkbox,
I have table with pageId, parentPageId, title columns. Is there a way to return

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.