Forum: Ruby on Rails Cleaning up a UGLY method.

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Vincent F. (Guest)
on 2007-06-23 23:11
(Received via mailing list)
I've been playing with a horrid method that I am using to sort my
Projects by the average importance of their tasks. it hits the
database quite a bit and is very ugly... does anyone have any
suggestions? rails is still very new to me.

here is my method.

   def self.list_by_priority
     priority = [ 5,4,3,2,1 ]
     sorted =
     priority.each do | current_priority |
       find(:all).each do |project|
          sorted << project if project.tasks.average(:priority) >=
current_priority && sorted.include?(project) == false
Michael D. Ivey (Guest)
on 2007-06-23 23:52
(Received via mailing list)
On Jun 23, 2007, at 2:10 PM, Vincent F. wrote:
>    end
How about:

class << self
   def list_by_priority
     find(:all, :include => :tasks).sort_by { |project|
project.tasks.average(:priority) }
This topic is locked and can not be replied to.