Complex sorting question, or not?

14 views
Skip to first unread message

Chris Klimek

unread,
May 7, 2012, 3:15:10 PM5/7/12
to rubyonra...@googlegroups.com
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!

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
May 11, 2012, 9:20:41 AM5/11/12
to rubyonra...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages