rails/rails — Issue #5051: ActiveRecord find_by_col1_and_col2 with hash parameters throws ArgumentError: wrong number of arguments (1 for 2)
Author: trisweb
Created: 2012-02-15 17:38:15+00:00
State: open

When using find_by_col1_and_col2_... (more than one column) the argument count check in active_record/lib/dynamic_matchers.rb method_missing fails because it attempts to compare an array with the argument hash, to an array of straight argument names from the method call.

Example:

Calling, `User.find_by_name_and_email(:name => "Tester", :email => "test@example.com")` results in:

arguments = `[{:name=>"Tester", :email=>"test@example.com"}]`
attribute_names = `["name", "email"]`

Such that the condition: `if arguments.size < attribute_names.size` always fails when attribute_names.size > 1

Workaround is to use new where syntax instead.

`User.where(:name => "Tester", :email => "test@example.com")`

Works great.

Initially discovered with `User.find_or_create_by_`, in which case the workaround is `User.where(...).first_or_create(...)`, which also works fine.

Rails 3.2.1, Ruby 1.8.7