Let's say I have two models: location and employee
- employee belongs_to locations - locations has_many employees
On a particular page of an app, I am looping through each location and then showing employees from that location.
In the controller, I am doing a search on all locations and listing them by location_id.
In the view, I am looping through each location, and showing the employee details for that location. However, the employees being listed need to be shown in alphabetical order, but are being shown ordered by id.
Since I am not flat-out searching for the employees in the controller, how do I perform a sort on the employees to get them to list alphabetically?
Here is the code I am using to loop through the locations and employees:
<table id="emp_list_layout"> <% @locations.each do |loc| %> <tr id="place"> <td colspan="5" class="locheader"><%= loc.name %></td> </tr> <% loc.employees.in_groups_of(5) do |emp| %> <tr> <% emp.compact.each do |member| %> <td><div id="memberlayout"> <%= link_to(image_tag( member.img_sml, :alt => member.title, :class => "empimg", :size => "42x45" ), { :controller => "profile", :action => "show", :id => member.id } ) %><br /> <%= member.first_name %> <%= member.last_name %></div></td> <% end %> </tr> <% end %> <% end %> </table>
Thanks for your help, Sarah