Complex sorting question, or not?

I have a function that displays all depts and related info from other tables in my Department model, such as:

  def dept_and_hospital     "#{name} (#{floor.building.hospital.name})"   end

... which is used in my view, like:

  <p>     <%= f.label :department_id %><br />     <%= collection_select(:device, :department_id, Department.all(), :id, :dept_and_hospital, :prompt => 'Select Department') %>   </p>

How do I sort the display names by the hospital name (which is a few "tables away" in the db schema), and then by the department name.

The relationship in the db is as follows: department-->floor-->building--> hospital.

THANKS!

Chris Klimek wrote in post #1059910:

I have a function that displays all depts and related info from other tables in my Department model, such as:

  def dept_and_hospital     "#{name} (#{floor.building.hospital.name})"   end

... which is used in my view, like:

  <p>     <%= f.label :department_id %><br />     <%= collection_select(:device, :department_id, Department.all(), :id, :dept_and_hospital, :prompt => 'Select Department') %>   </p>

First, get Department.all() out of your view. It doesn't belong there. It's not the responsibly of the view to load it's own data. Put that in the controller where it belongs.

How do I sort the display names by the hospital name (which is a few "tables away" in the db schema), and then by the department name.

The relationship in the db is as follows: department-->floor-->building--> hospital.

Given that it sounds like your list of results is small enough to fetch them all into memory, then you can sort them in memory using Ruby, I'll leave that as an exercise for you.

Sorting arrays: http://bit.ly/Kt6sI9