Getting "wrong number of arguments" error on finder method
24,350
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.
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])
Author by
Dave
Updated on July 09, 2022Comments
-
Dave almost 2 years
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
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.
-
Richard Peck over 8 yearsStill looks like 3 args to me?... although you are right with their declarations
-
miler350 over 8 yearsIt's supposed to be 3 args:
(wrong number of arguments (given 1, expected 3))
. He was submitting only 1 argument (the hash)