7

Say you have an ordered array like this, generated from a database of addresses:

[
  { city: Sacramento, state: CA },
  { city: San Francisco, state: CA },
  { city: Seattle, state: WA }
]

And you want to generate HTML with it like this:

<p>CA</p>
<ul>
    <li>Sacramento</li>
    <li>San Francisco</li>
</ul>
<p>WA</p>
<ul>
    <li>Seattle</li>
</ul>

So you're grouping by state. One approach to doing this would be to remember the last row on each iteration of the loop and to display the state and bookending UL tags only if the current row's state is the same as the last rows state. That seems kind of nasty and non Ruby-y.

Anyone have any advice on an elegant Ruby/Rails approach to this?

3 Answers 3

9

Enumerable has group_by

cities = [
  { city: "Sacramento", state: "CA" },
  { city: "San Francisco", state: "CA" },
  { city: "Seattle", state: "WA" }]

cities.group_by {|c| c[:state]}


=> {"CA"=>[{:city=>"Sacramento", :state=>"CA"}, 
           {:city=>"San Francisco", :state=>"CA"}], 
    "WA"=>[{:city=>"Seattle", :state=>"WA"}]}

I'm kind of rusty on ERB but I think it would be something like this

<% @cities_by_state.each do |state, cities| %>
<p><%= state %></p>
<ul>
  <% cities.each do |city| %>
    <li><%= city[:city] %></li>
  <% end %>
</ul>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

8

Enumerable#group_by ?

array = [
  {city: 'Sacramento', state: 'CA'},
  {city: 'San Francisco', state: 'CA'},
  {city: 'Seattle', state: 'WA'}
]

array.group_by{|elem| elem[:state]}
# => {"CA"=>[{:city=>"Sacramento", :state=>"CA"}, {:city=>"San Francisco", :state=>"CA"}], "WA"=>[{:city=>"Seattle", :state=>"WA"}]} 

Comments

0

You can use the group_by function in Rails

@records.group_by{|x| x[:state]}

This will return you a Hash where the key is the state and the values are an array of the records

This link should help you figure out how it works a little more.

3 Comments

You can't use Symbol#to_proc to reference a symbol hash key. ;)
@coreyward: You can if your elements are AR objects, not Hashes. ;)
@Mladen Jablanović Yes, then they are methods, not symbol hash keys. Short of the missing quotation marks delimiting the hash values as strings, the question shows a valid Ruby hash definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.