Ruby hashes of status counts
You can use Array#group_by to produce a hash keyed by status. Then follow that by a map to collect the counts. You'll get an array of key/value pairs, so use Hash[] to make that into a proper hash:

def batch_status
  counts = self.batch_details.group_by(&amp;:status).map { |k, v| [k, v.count] }
  Hash[counts]
end


Alternatively, you can use use your current approach, just shortened a bit

def batch_status
  self.batch_details.reduce({}) do |counts, details|
    counts[details.status] = (counts[details.status] || 0) + 1
  end
end




Edit: Completely overlooked the "count" key. I muddies the picture a little, but you could do something like this for code blocks 1 and 2 above

def batch_status
  counts = self.batch_details.group_by(&amp;:status).map { |k, v| [k, v.count] }
  counts &lt;&lt; ["count", self.batch_details.count]
  Hash[counts]
end


and

def batch_status
  counts = { "count" =&gt; self.self.batch_details.count }
  self.batch_details.reduce(counts) do |counts, details|
    counts[details.status] = (counts[details.status] || 0) + 1
    counts
  end
end


Or, as Cary Swoveland suggests:

def batch_status
  counts = { "count" =&gt; self.self.batch_details.count }
  counts.default = 0
  self.batch_details.reduce(counts) do |counts, details|
    counts[details.status] += 1
    counts
  end
end


Which cleans it up nicely. Further cleanup could be done by using each_with_object in place of reduce:

def batch_status
  counts = { "count" =&gt; self.self.batch_details.count }
  counts.default = 0
  self.batch_details.each_with_object(counts) do |counts, details|
    counts[details.status] += 1
  end
end