Cleaning up a UGLY method


#1

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 = Array.new
priority.each do | current_priority |
find(:all).each do |project|
sorted << project if project.tasks.average(:priority) >=
current_priority && sorted.include?(project) == false
end
end
sorted
end


#2

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) }
end
end