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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:11:09+00:00 2026-05-27T14:11:09+00:00

I have an array of objects that looks like this: [#<Holding id: 5, user_id:

  • 0

I have an array of objects that looks like this:

[#<Holding id: 5, user_id: 21, outcome_id: 7, quantity: 200, cost_basis: nil, created_at: "2011-12-15 20:52:05", updated_at: "2011-12-15 20:52:05">,
 #<Holding id: 6, user_id: 21, outcome_id: 7, quantity: 750, cost_basis: nil, created_at: "2011-12-15 21:31:30", updated_at: "2011-12-15 21:31:30">,
 #<Holding id: 7, user_id: 21, outcome_id: 9, quantity: 1231, cost_basis: nil, created_at: "2011-12-15 21:35:31", updated_at: "2011-12-15 21:35:31">]

I retrieved this by using my model’s find_all_by_user_id (Holding.find_all_by_user_id(21)). What I need, however, is an array of sums. So, for instance, what I’d like to get instead in this instance is:

[#<Holding id: 5, user_id: 21, outcome_id: 7, quantity: 950, cost_basis: nil, created_at: "2011-12-15 20:52:05", updated_at: "2011-12-15 20:52:05">,
     #<Holding id: 7, user_id: 21, outcome_id: 9, quantity: 1231, cost_basis: nil, created_at: "2011-12-15 21:35:31", updated_at: "2011-12-15 21:35:31">]

What would make it even better is if I could pull from my Outcomes model the Outcome.description and have it in this second array along with the id. Do I just have to loop over and create sums where outcome_id’s match or is there a better way?

  • 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-27T14:11:09+00:00Added an answer on May 27, 2026 at 2:11 pm

    Personally, I would do this in SQL – SUM(quantity) and GROUP BY outcome_id.

    If you want it in Ruby, you can do

    holdings = Holding.find_all_by_user_id(21)
    
    sums = Hash.new(0)
    
    holdings.each do |h|
      sums[h.outcome_id] += h.quantity
    end
    

    sums is now a hash with key being all the outcome_ids and the value being the total quantity.

    Please note that in both solutions (SQL and Ruby) you will lose the information in the other fields (holding_id, user_id, cost_basis, timestamps), which may not be what you want.

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

Sidebar

Related Questions

I have an array of ruby objects that looks something like this: [#<email: someemail
I have an object that looks like this when outputted via a print_r Array
I have a list of objects MyObject and that looks like this: var Obj1
I have a vector-like class that contains an array of objects of type T
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a Json file that looks like this: [ { field:val }, ....
We have a model association that looks something like this: class Example < ActiveRecord::Base
I have an object that looks like this: var MyObject = { prop1 =
So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id

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.