I have a School and Result model where results belong to schools, and schools have results for every year. Each result has a field :score, and I am trying to rank each 'result' by its score but only for each year. So there will be different rankings for each different year.
1) What is the best way to achieve this ranking?
One approach I have thought of is to run a cron job every 10 minutes (for example) that updates the rank
update_rank.rake
task :build => :environment do
Result.where(year: year).desc(:score).each_with_index do |result, position|
result.update_attribute(:rank, position + 1)
end
end
I have an issue here though:
2) How can I do the ranking for each different year? I could do something like below, but it seems a little bulky, is there anything else i could do?
task :build => :environment do
eachyear = (1990..(Time.now.year)).to_a
eachyear.each do |year|
Result.where(year: year).desc(:score).each_with_index do |result, position|
result.update_attribute(:rank, position + 1)
end
end
end