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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/c3ioHehytp8J. For more options, visit https://groups.google.com/groups/opt_out.
On 22 August 2012 17:26, ngw <ngw-joN+ekMQmMEdnm+yROfE0A@public.gmane.org> wrote:> 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.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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi! Take a look at the method group_by: http://www.ruby-doc.org/core-1.9.3/Enumerable.html#method-i-group_by On Wed, Aug 22, 2012 at 1:44 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 22 August 2012 17:26, ngw <ngw-joN+ekMQmMEdnm+yROfE0A@public.gmane.org> wrote: > > 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. > > 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 > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.