>   def self.by_high_score
>     self.all(
>              :select => 'teams.id, teams.name, max(games.score)',
>              :joins => 'left join games on team_id = teams.id',
>              :group => 'teams.id',
>              :order => 'score desc',
>              :conditions => 'score is not null'
>     )
>   end
While this will probably work just fine, I would recommend caching the 
high score in a attribute of the Team model. This would be very similar 
to the counter_cache feature that Rails provides. This way your sorting 
could be handled by a very simple named route. It would also be many 
times more efficient since there is no join to deal with.

The trade-off is that you need additional logic to update the cached 
value when necessary, and in making sure that you always rely on your 
model's business logic to manage the cache. I personally think that the 
performance gained though this technique is well worth the trade-off in 
case where performance may be a significant factor.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to