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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:41:59+00:00 2026-05-20T01:41:59+00:00

I have a table of Logs with the columns name, duration, type, ref_id .

  • 0

I have a table of Logs with the columns name, duration, type, ref_id.

I update the table every so often so perhaps it will look like a col of ['bill', 'bob', 'bob', 'jill'] for names, and [3, 5, 6, 2] for duration, and ['man', boy', 'boy', 'girl'] for type, and [1, 2, 2, 3] for ref_id.

I would like to manipulate my table so that I can add all the durations so that I get a hash or something that looks like this:

{'name' => ['bill', 'bob', 'jill'], 'duration' => [3, 11, 2], 'type' => ['man', 'boy', 'girl'], ref_id => [1, 2, 3]}

How can I do this?

(for more info–currently I’m doing Log.sum(:duration, :group => 'name') which gives me the names themselves as the keys (bill, bob, jill) instead of the column name, with the correct duration sums as their values (3, 11, 2). but then I lose the rest of the data…)

I guess I could manually go through each log and add the name/type/ref_id if it’s not in the hash, then add onto the duration. If so what’s the best way to do that?

If you know of good sources on rails array manipulation/commonly used idioms, that would be great too!

  • 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-20T01:42:00+00:00Added an answer on May 20, 2026 at 1:42 am

    Couple of notes first.

    Your table is not properly normalized. You should split this table into (at least) two: users, and durations. You should do this for lots of reasons, that’s relational databases 101.

    Also, the hash you want as a result also doesn’t look right, it suggests that you are pre-grouping data to suit your presentation. It’s usually more logical to put these results in an array of hashes, than in a hash of arrays.

    Now on to the answer:

    With your table, you can simply do GROUP BY:

    SELECT name, type, ref_id, SUM(duration) as duration
    FROM logs
    GROUP BY name, type, ref_id
    

    or, using AR:

    durations = Log.find(:all,
      :select => 'name, type, ref_id, SUM(duration) as duration',
      :group => 'name, type, ref_id'
    )
    

    In order to convert this to a hash of arrays, you’d use something like:

    Hash[
      %w{name, type, ref_id, duration}.map{|f|
        [f, durations.map{|h|
          h.attributes[f]
        }]
      }
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with 50 entries (users with such details like Name Surname Location
I have a table with 4 columns, id , Stream which is text, Duration
i have a table with columns: ID_LOG (this is the pk) | TS (type=datetime)
Let's say that I want to have a table that logs the date and
I have the following need I have a logging table which logs som leads
I have a simple table in MySql whose raison-d'être is to store logs. The
I have two tables (in postgres) - ads and logs. After every insert into
I have table with some fields that the value will be 1 0. This
I have table with 3 columns A B C. I want to select *
I have a simple table that logs simple events an administrator does. Nothing complicated.

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.