user984621
user984621

Reputation: 48463

Ruby - how to effectively fetch and group certain information from an object?

I have an object containing tens of thousands orders. The object looks like this:

[#<Order id: 4304, order_code: 'AB11', manufacturer_id: 24, ...>, 
 #<Order id: 7664, order_code: 'AB13', manufacturer_id: 24, ...>, 
 #<Order id: 4164, order_code: 'AB255', manufacturer_id: 724, ...>, 
 #<Order id: 7671, order_code: 'AX88', manufacturer_id: 855, ...>,
...]  

Now, what I am trying to get - I'd like to get an object looking like this (or something similar):

[ 
  {manufacturer_id: 24, orders: [{id: 4304, order_code: 'AB11'}, {id: 7664, order_code: 'AB13'}]},
  {manufacturer_id: 724, orders: [{id: 4164, order_code: 'AB255'}]},
  ...
]

What is the most effective way to extract that information from the given Order object? This will be pulled off couple times a day.

Thank you.

Upvotes: 0

Views: 106

Answers (2)

zhisme
zhisme

Reputation: 2800

I would end up some presenter, something like PORO (plain old ruby object)


class OrderPresenter
  attr_reader :records

  def initialize(records)
    @records = records
  end

  def as_json
    records.group_by(&:manufacturer_id).map do |k, v| 
      { 
        manufacturer_id: k, 
        orders: v.collect { |o| {id: o.id, order_code: o.order_code } }  
      }      
    end  
  end
end

And I don't think you need them all at once? So you can do something like partial load, not to blow up all your memory

records.each_slice(1000) do |group|
  data = OrderPresenter.new(records).as_json
  # perform something on that data
end

Upvotes: 0

Ritesh Choudhary
Ritesh Choudhary

Reputation: 782

You can do something like this

list.group_by(&:manufacturer_id).map do |k,v| 
  { 
    manufacturer_id: k, 
    orders: v.collect{ |o| {id: o.id, order_code: o.order_code }}  
  }      
end  

output look like below

=> [{:manufacturer_id=>24, :orders=>[{:id=>4304, :order_code=>"AB11"}, {:id=>7664, :order_code=>"AB13"}]}, {:manufacturer_id=>724, :orders=>[{:id=>4164, :order_code=>"AB255"}]}, {:manufacturer_id=>855, :orders=>[{:id=>7671, :order_code=>"AX88"}]}]

Upvotes: 1

Related Questions