-
-
Save anonymous/da8d811e0d8033c1b0a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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