Displaying an array of AR objects alphabetically

34 views
Skip to first unread message

ngw

unread,
Aug 22, 2012, 12:26:46 PM8/22/12
to rubyonra...@googlegroups.com
Hi *, I have an array of AR objects that I've been able to sort alphabetically that I need to split by letter, for example { :a => [apple], :b => [banana] } and so on, based on an attribute that I use for sorting.
The end result should be something like

<ul>
  <li>A</li>
  <ul>
    <li> Apple </li>
  </ul>
  <li>B</li>
  <ul>
    <li> Banana </li>

and so on.
I have no idea how to do this...
The array is already sorted alphabetically using the DB.

Can someone help me?
  ngw

Colin Law

unread,
Aug 22, 2012, 12:44:23 PM8/22/12
to rubyonra...@googlegroups.com
Is it that you do not know how to write the ruby code to iterate the
records? If your objects are in @records then you can use
@records.each.

Colin

Everaldo Gomes

unread,
Aug 22, 2012, 1:06:10 PM8/22/12
to rubyonra...@googlegroups.com
Hi!

Take a look at the method group_by:


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages