Forum: Ruby on Rails Displaying an array of AR objects alphabetically

Posted by ngw (Guest)
on 2012-08-27 14:01
(Received via mailing list)
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
Posted by Colin Law (Guest)
on 2012-08-27 14:04
(Received via mailing list)
On 22 August 2012 17:26, ngw <ngw@nofeed.org> wrote:
>   <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.

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
Posted by Everaldo Gomes (Guest)
on 2012-08-27 14:06
(Received via mailing list)
Hi!

Take a look at the method group_by:

http://www.ruby-doc.org/core-1.9.3/Enumerable.html...
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.