Forum: Ruby on Rails Cleaning up a UGLY method.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3b15a36c3ac77a31f3296d04bbe0d408?d=identicon&s=25 Vincent Franco (Guest)
on 2007-06-23 21: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 = 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
F5d61a3c93217e393cfdf7bf5c7ac628?d=identicon&s=25 Michael D. Ivey (Guest)
on 2007-06-23 21:52
(Received via mailing list)
On Jun 23, 2007, at 2:10 PM, Vincent Franco wrote:
>    end
How about:

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