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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:33:37+00:00 2026-05-20T00:33:37+00:00

So I have a array of records and I would like to group by

  • 0

So I have a array of records and I would like to group by 2 levels

Essentially I would like to group_by{|x| x.field1 } and then each value in hash to be further grouped in by field2. Effectively leading to a tree that I can dump out.

def treemaker(array = [])
  tree = ledgers.group_by{|x|x.master_group}
  tree.each{|x,z| tree[x] = z.group_by{|y| y.account_group}}
  tree
end

I would then render tree in a way that i can be put into a “tree” javascript plugin.

Is there a more efficient way?

Sample Input: An Array of ActiveRecord objects, where the model contains, fields master_group, account_group and name

Class MyModel < ActiveRecord::Base
  validates :master_group, :account_group, :name, :presence => true
end

Sample Ouput:

{"master_group1" => {"account_group1" => ["name1","name2",...], 
                     "account_groupx" => ["name3", "name4",...],
                     ....}, 
 "master_group2" => {"account_group2" => ["namex", "namey"]},
 ...
}

I’m not specifically looking for an “SQL grouping” solution (but that would be nice too). Just a solution using enumerables on a any given list of ruby objects.

  • 1 1 Answer
  • 1 View
  • 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-20T00:33:37+00:00Added an answer on May 20, 2026 at 12:33 am

    @xaxxon sent me thinking in the right way basically with the “default value of hash” path.

    I think i can now add a method to my model where i can use all sorts of scopes and tack on tree at the end to get my models in tree mode.

    class MyModel < ActiveRecord::Base
    
      validates :master_group, :account_group, :name, :presence => true
      def self.tree(field1 = 'master_group', field2 = 'account_group')
        tree = Hash.new{|hash,key| hash[key] = Hash.new{|h,k| h[k] = []}}
        all.each do |item|
          tree[item.send('field1')][item.send('field2')].push(item)
        end
        tree # bob's your uncle!
      end
    
    end
    
    MyModel.recent.tree => Hash of Hash of arrays
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a group of images which I would like to display in a
I have a column called 'comments', and would like to find all records containing
I have a service which returns an Array of ObjectProxy objects. I would like
I would like to save several records for one model. This would have been
I have two PHP arrays like so: Array of X records containing the ID
I have about 10-20K records and only need certain cities. I would like to
I would like to initialize a select with an initial value. I have a
I would like to have an array model objects to be serialized to a
I would like to have a custom function to insert/edit/delete records from a database
I have this (I'm only showing three records, there are many, many more) Array

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.