1

I am generating a list of centres, but because the centres are a child of borough, I was wondering how to sort the iteration alphabetically in the views.

My code looks like this

        <% boroughs.each do |borough|%>
          <% if borough.leisure_centres.any? %>
            <% borough.leisure_centres.each do |venue| %>
            <li class="sitemap-accordion">
              <%= link_to venue.title, '#', class: "trigger" %>
                <ul class="sitemap-accordion-child">
                  <li>
                    <%= link_to "Overview", centre_path(venue) %>
                  </li>
                  <li>
                    <%= link_to 'News', centre_news_index_path(venue) %>
                  </li>
                  <li>
                    <%= link_to 'Facilities', facilities_centre_path(venue) %>
                  </li>
                  <li>
                    <%= link_to 'Contact Us', new_centre_contact_form_path(venue) %>
                  </li>
                  <% unless venue.venue_hire_content.nil? %>
                    <li>
                      <%= link_to 'Hire', venue_hire_centre_path(venue) %>
                    </li>
                  <% end %>
                  <% unless venue.virtual_tour.nil? %>
                    <li>
                      <%= link_to 'Virtual Tour', tour_centre_path(venue) %>
                    </li>
                  <% end %>
                  <% unless venue.custom_pages.nil? %>
                    <% venue.custom_pages.each do |custom_page| %>
                      <li>
                        <%= link_to custom_page.title, centre_custom_page_path(centre_id: venue, id: custom_page.id) %>
                      </li>
                    <% end %>
                  <% end %>
                </ul>
              <% end %>
            </li>
          <% end %>
        <% end %>

I would like venue.title to be in alphabetical order, how do i do this?

Thanks

2
  • leisure_centre is it's own model?
    – dax
    Jul 10, 2014 at 11:52
  • good shout homeslice, so, how do i do it in the views?
    – rico_mac
    Jul 10, 2014 at 11:58

2 Answers 2

3

You can pass a block to sort_by method like this.

<% borough.leisure_centres.sort_by(&:title).each do |venue| %>
3
  • 1
    beat me by 11 seconds!
    – dax
    Jul 10, 2014 at 12:00
  • yeah. That was close :P
    – Santhosh
    Jul 10, 2014 at 12:01
  • yeah, but that sorts the leisure centres by boroughs?
    – rico_mac
    Jul 10, 2014 at 12:08
2

I would do

Rails 4

class Borough < ActiveRecord::Base
  has_many :leisure_centres, -> { order "title asc" }
end

Rails 3

class Borough < ActiveRecord::Base
  has_many :leisure_centres, :order => "title asc"
end

So you are sorting via SQL not ruby.

5
  • goo shout, but "syntax error, unexpected ->, expecting keyword_end has_many :leisure_centres -> { order "title asc" }"
    – rico_mac
    Jul 10, 2014 at 12:10
  • Which version of ruby are you using?
    – j-dexx
    Jul 10, 2014 at 12:12
  • ahh, i changed to => i am ona legacy project so the rails vis a tad older. still doesn,t work though
    – rico_mac
    Jul 10, 2014 at 12:15
  • Which version of rails then? and why have you tagged it rails 4 if you're not using rails 4?
    – j-dexx
    Jul 10, 2014 at 12:15
  • my bad, ill change it, rails 3.2
    – rico_mac
    Jul 10, 2014 at 12:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.