Hello,
Is it possible to order a result set by one of the values in an habtm
relationship? If so, how can this be done?
For example, I have a data set of providers.
@levels = Level.find(:all)
@pages, providers = paginate(:providers, :conditions => [“last_name LIKE
?”, @initial+’%’], :order => “last_name, first_name”)
@providers = providers.collect { |provider| provider }
In the model, providers has_and_belongs_to_many levels, and levels
has_and_belongs_to_many providers. Is it possible, whether in the view
or in the controller, to cause the query to sort itself by levels? So,
it would display the providers associated within a given level, and then
sort by whatever is in the controller.
Currently, the view is like this:
<% if @pages %>
<% for provider in @providers %>
(enter values for the provider in this area. I can get it to display
values from the level by doing this:)
<% for level in provider.levels do %><%= level.name.downcase %><% end %>
<% end %>
<% end %>
Any thoughts would be most appreciated. Thanks.
Jonathan