首页 > 解决方案 > ArgumentError:参数数量错误(给定 0,预期 2..3)

问题描述

我正在尝试让 searchkick 工作,但是当我尝试通过控制台重新索引我的故事遇到错误。我似乎无法理解错误可能是什么,并且希望得到一些帮助。

输入“Story.reindex”后通过控制台报错:

Story Import (3.6ms)  {"count":2,"exception":["ArgumentError","wrong 
number of arguments (given 0, expected 2..3)"],"exception_object":"wrong 
number of arguments (given 0, expected 2..3)"}
ArgumentError: wrong number of arguments (given 0, expected 2..3)
    from app/models/story.rb:42:in `hot_score'
    from (irb):1

story.rb 模型:

class Story < ApplicationRecord
searchkick

has_many :votes

scope :newest, -> { order(created_at: :desc) }
scope :hottest, -> { order(hot_score: :desc) }

def upvotes
 votes.sum(:upvote)
end

def downvotes
 votes.sum(:downvote)
end

def calc_hot_score
 points = upvotes - downvotes
 time_ago_in_hours = ((Time.now - created_at) / 3600).round
 score = hot_score(points, time_ago_in_hours)

 update_attributes(points: points, hot_score: score)
end

private

def hot_score(points, time_ago_in_hours, gravity = 1.8)
 (points - 1) / (time_ago_in_hours + 2) ** gravity
end
end

任何人都可以帮助解释正在发生的事情以及解决它的方法吗?我对 Ruby 还是很陌生。

提前谢谢你。

标签: ruby-on-railsruby

解决方案


推荐阅读