Tuesday, September 6, 2011

Getting error "ArgumentError: wrong number of arguments (3 for 2)" for mysql2_adapter

In one of my application I am using Ruby 1.9.2, mysql2 gem, Rails 3.0.6.

While upgrading to Rails 3.1, getting following error throughout the application. When I ran command on console, then also getting same error:

ruby-1.9.2-p290 :003 > c = Cuisine.all
ArgumentError: wrong number of arguments (3 for 2)
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/mysql2-0.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:585:in `select'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:470:in `find_by_sql'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/relation.rb:111:in `to_a'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/relation/finder_methods.rb:155:in `all'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:441:in `all'
    from (irb):3
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Solution to fix the problem:

In Gemfile, replace 'mysql2' line with

       gem 'mysql2', '>= 0.3'

and install mysql2 greater than '0.3' version, in my case it is ' mysql2 (0.3.6) ' and it will work :)


NOTE: as of Rails 0.3.0, and ActiveRecord 3.1 - the ActiveRecord adapter has been pulled out of mysql2 gem and into ActiveRecord itself. If you need to use mysql2 with Rails versions < 3.1 make sure and specify gem "mysql2", "~> 0.2.7" in your Gemfile

And for Rails version 3.1 or above use  gem 'mysql2', '>= 0.3'

2 comments:

  1. Proof once again that Googling for error messages always leads to a blog with the solution -- Thanks for posting this!

    ReplyDelete
  2. Just a note to possibly save someone - you get the same error by using recent sql_logging plugin with rails 3.1. I have forked, corrected and requested pull on github, so it should be fine soon (details: https://github.com/lightyear/sql-logging/pull/5#issuecomment-3177597)

    ReplyDelete