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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jun 23, 2007, at 2:10 PM, Vincent Franco wrote:> 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 > endHow about: class << self def list_by_priority find(:all, :include => :tasks).sort_by { |project| project.tasks.average(:priority) } end end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---