Home > database >  Ruby how to format nested hash
Ruby how to format nested hash

Time:11-10

I have two queries I am running and iterating over both and my final hash is seen below. But, I want to have format on how the data is being stored in the hash that I'm creating or format it after I'm done creating it. But I am not sure how to achieve the desired format where the names fall under the same id as show below

desired format of example data:

[
    {
        id: 1,
        accepted: false,
        trans: 10234
        names: [
            { name: "Joe", amount: "$1,698.00" },
            { name: "Smith", amount: "$674.24" },
        ]
    },
    {
        id: 2,
        accepted: true,
        trans: 10234,
        names: [
            { name: "Joe", amount: "$1,698.00" },
            { name: "Smith", amount: "$674.24" },
        ]
    }
]

current format I have

[
    {
               :id => 1,
               :accepted => false,
               :trans => 8,
               :name => "Smith",
               :amount => 36.0
    },
    {
               :id => 1,
               :amount => false,
               :trans => 8,
               :name => "Joe",
               :amount => 6.0
    },
    {
               :id => 3,
               :accepted => false,
               :trans => 8,
               :name => "Tom",
               :amount => 34.0
    },
     {
               :id => 3,
               :accepted => false,
               :trans=> 8,
               :name => "Martha",
               :amount => 4.0
    }
], 
[
    {
               :id => 2,
               :accepted => true,
               :trans => 7,
               :name => "Bob",
               :amount => 35.0
    },
     {
                :id => 2,
                :accepted => true,
                :trans => 7,
                :name => "John",
                :amount => 5.0
    }
]

logic for creating hash

imports = ListImports.limit(20).order(created_at: :DESC)
groups = imports.map{|import| ListImportGroup.where(list_import_id: import.id)}
pub_hash_true = []
pub_hash_false = []
hash = []
imports.map do |import|
  hash << {
     id: import.id,
     trans: import.trans,
     accepted: import.amount
  }
end
  hash.each do |import|
    groups.flatten.each do |group|
      accepted = import[:accepted]
      num_transactions = import[:trans]
      if accepted == false
        pub_hash_false << {id: import[:id], accepted: accepted, trans: num_transactions, name: group.name, amount: group.amount}
      else
        pub_hash_true << {id: import[:id], accepted: accepted, trans: num_transactions, name: group.name, amount: group.amount}
      end
    end
  end

CodePudding user response:

# Note: You didn't specify what is the association between `ListImport` and `ListImportGroup`.
# However, I'm fairly sure you could be fetching this data via a JOIN query like below,
# rather than making up to 20 additional database calls to fetch the associated records.

imports = ListImports.limit(20).order(created_at: :DESC).includes(:list_import_group)

result = imports.map do |import|
  {
    id: import.id,
    trans: import.trans,
    accepted: import.amount,
    names: import.list_import_groups.pluck(:name, :amount)
  }
end

And if you do actually need to filter for imports where accepted is true or false, you could do something like this instead of building separate arrays manually:

accepted_imports = result.select { |import| import[:accepted] }
# and
rejected_imports = result.reject { |import| import[:accepted] }

# or even:
accepted_imports, rejected_imports = result.partition { |import| import[:accepted] }

CodePudding user response:

You didn't specify the exact correspondence between the desired and current formats. But I assume

  • For the entries with the same id, the values of accepted and trans are identical.
  • the desired amount for Joe in the current format is identical in the corresponding amount in the desired amount. (In your example, the former is 6.0 whereas the latter is "$1,698.00", which does not make sense.)

Then, the following would do the conversion. The array ahout is in the desired format.

 # Let us assume "a1" is the original array in the "current format"
hout = {}
a1.flatten.map{|h|
   h.slice(*(%i(id trans name amount accepted))).values
}.each{ |a|
  hout[a[0]] = {id: a[0], accepted: a[4], trans: a[1], names: []} if !hout.key? a[0]
  hout[a[0]][:names].push(
    {name: a[2], amount: "$" helper.number_with_precision(a[3], precision: 2, delimiter: ',')}
  )
}

ahout = hout.values

You may want to sort ahout, if you like.

Note that I am assuming you are using Rails 5 . Otherwise, the method helper may not work. In that case, you can use sprintf or whatever formatting method.

  • Related