用新排序的问题替换旧排名问题的方法

时间:2010-11-03 02:02:56

标签: ruby ruby-on-rails-3

    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

1 个答案:

答案 0 :(得分:0)

在第一个#each_with_index

中缺少'do'

https://gist.github.com/da8d811e0d8033c1b0a9