简体   繁体   中英

Using group_by but return an array of hashes

I simply want to group cities by their state and from there have the array of the hash key (ie State Name ) return an array of hash data pertaining to it's cities. Right now I have something like this:

City.all.group_by { |c| c.state.name }

Which will return:

{
  "Illinois": [# < City id: 3, name: "Chicago", state_id: 3 > ],
  "Texas": [# < City id: 2, name: "Houston", state_id: 2 > ],
  "California": [# < City id: 1, name: "Los Angeles", state_id: 1 > ],
  "New York": [# < City id: 4, name: "New York City", state_id: 4 > ]
}

Notice how it returns an array of rails objects. Instead I want to return an array of hashes with certain attributes, like their id and name .

The reason the grouped values are Rails objects (your models) is due to the fact that you also start with these objects. You can use the attributes method to retrieve the attributes of a model instance as a hash.

The following achieves the result you want:

City.all.group_by { |city| city.state.name }
  .transform_values { |cities| cities.map(&:attributes) }

If you only want specific attributes, use slice instead:

City.all.group_by { |city| city.state.name }
  .transform_values { |cities| cities.map { |city| city.slice(:id, :name) } }

Note that slice will return an ActiveSupport::HashWithIndifferentAccess instance. Which mostly can be used in the same manner as a normal hash, but returns the same value for both hash[:name] and hash['name'] . If you rather use a normal hash append a to_hash call after the slice call.

This should be enough for you

City.all.group_by { |c| c.state.name }.map {|k,v| [k, v.attributes] }.to_h

and to select only specified attributes do

v.attributes.slice(:name, :id)

One of the easiest approach is to convert it into json object

City.all.as_json.group_by { |c| c.state.name }

this will fix the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM