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 |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
SyntaxError:(irb):275:语法错误,意外'}'
(irb):280:语法错误,意外的tIDENTIFIER,期待':'
... newrankings.push(a)oldrankings.delete_at(i):next}
...... ^
(irb):280:语法错误,意外':',期待'}'
...... h(a)oldrankings.delete_at(i):next}
...... ^
(irb):284:语法错误,意外的keyword_end,期待'}'
来自lib / rails / commands / console.rb:44:in start'
from lib/rails/commands/console.rb:8:in
start'
来自lib / rails / commands.rb:23:in <top (required)>'
from script/rails:6:in
require'
来自script / rails:6:in''
Id:Integer
Content:String
Rank:Integer #Normally Null unless ranked in which case you'll have a rank of 1 to 5