Ruby-on-rails 在finder方法上出现“错误数量的参数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35237024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:22:49  来源:igfitidea点击:

Getting "wrong number of arguments" error on finder method

ruby-on-railsmodelargumentsfinder

提问by Dave

I'm using Rails 4.2.3 (and using MySQL 5.5.37). I'm having difficulty writing a finder method for one of my models. I have columns “user,” “object,” and “day”, but the following

我正在使用 Rails 4.2.3(并使用 MySQL 5.5.37)。我在为我的一个模型编写 finder 方法时遇到了困难。我有“用户”、“对象”和“日”列,但以下内容

  def find_by_user_object_and_day
    respond_to do |format|
      @current_user = User.find(session["user_id"])
      format.js {
        render :text => Userobject.find_by_user_and_object_and_day(:user => @current_user, :object => params[:object], :day => params[:day]) 
      }
    end
  end

produces the error

产生错误

F, [2016-02-05T16:49:42.934112 #12058] FATAL -- : 
ArgumentError (wrong number of arguments (given 1, expected 3)):
  app/controllers/user_objects_controller.rb:77:in `block (2 levels) in find_by_user_object_and_day'
  app/controllers/user_objects_controller.rb:74:in `find_by_user_object_and_day'

How do I properly specify the arguments to the finder method? I haven't explicitly defined that finder method because I thought the “and” syntax would work.

如何正确指定 finder 方法的参数?我没有明确定义那个 finder 方法,因为我认为“and”语法会起作用。

采纳答案by miler350

The error says:

错误说:

ArgumentError (wrong number of arguments (given 1, expected 3))

This means you're supposed to pass in 3 distinct arguments, but you passed in 1. It looks like you passed in a hash of values instead of standalone values.

这意味着您应该传入 3 个不同的参数,但您传入了 1 个。看起来您传入的是散列值而不是独立值。

Replace:

代替:

 Userobject.find_by_user_and_object_and_day(:user => @current_user, :object => params[:object], :day => params[:day])

With this:

有了这个:

Userobject.find_by_user_and_object_and_day(@current_user, params[:object], params[:day])

回答by Richard Peck

find_by_some_column_and_another(dynamic finders) have been deprecated in favour of find_byin Rails 4+:

find_by_some_column_and_another(动态查找器)find_by在 Rails 4+ 中已被弃用:

@user = UserObject.find_by user: @current_user, object: params[:object], day: params[:day]


Using the above code structure shouldsolve your issue (find_bytakes a hash and requiresonly 1argument).

使用上面的代码结构应该可以解决你的问题(find_by需要一个散列并且只需要1参数)。

If you still want to use your dynamic find method, you need to ensure you have both the params[:object]and params[:day]values populated, as well as dropping the user:/object:/day:declarations (as mentioned by @miler350)

如果你仍然想使用动态查找方法,你需要确保你有两个params[:object]params[:day]值填充,以及丢弃user:/ object:/day:声明(如提及@miler350



Secondarily, you also need to make sure that you're constructing your actions properly:

其次,您还需要确保正确构建您的操作:

def find_by_user_object_and_day
    @current_user = User.find session["user_id"]
    @user_object  = UserObject.find_by user: @current_user, object: params[:object], day: params[:day] 

    respond_to do |format|
      format.js { render text: @user_object }
    end
end