Skip to content

Instantly share code, notes, and snippets.

/test.rb Secret

Created November 3, 2010 02:49
Show Gist options
  • Save anonymous/da8d811e0d8033c1b0a9 to your computer and use it in GitHub Desktop.
Save anonymous/da8d811e0d8033c1b0a9 to your computer and use it in GitHub Desktop.
def replaceranked
oldrankings = Question.where(:rank => [1,2,3,4,5])
# Mocking the creation of a new top 5
newrankings = Question.where(:id => [4,5,6,7,8])
newrankings.each_with_index do |a, i|
a.rank = i+
end
oldids = oldrankings.map{|a| a.id}
newids = newrankings.map{|a| a.id}
simlarities = oldids & newids
oldrankings.each_with_index do |a, i|
if !(similarities.includes?(a.id))
newrankings.push(a)
oldrankings.delete_at(i)
end
end
oldrankings.map{|a| a.rank = nil}
updatedtopfive = newrankings | oldrankings
updatedtopfive.map{|a| a.save}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment