5

I'm trying to build a Rails app around a pre-existing MySQL database. I created the rails project, set up the database.yml file properly and created one model, User, to correspond to my users table in the db. When I run the rails console to test it out however, this is what I get:

 > User.all
(32.4ms)  SHOW TABLES
(34.9ms)  describe `users`
NoMethodError: undefined method `accept' for nil:NilClass
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `to_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:16:in `select_all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/explain.rb:40:in `logging_query_plan'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:37:in `find_by_sql'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:170:in `exec_queries'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:159:in `block in to_a'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/explain.rb:33:in `logging_query_plan'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation.rb:158:in `to_a'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/relation/finder_methods.rb:159:in `all'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/querying.rb:5:in `all'
from (irb):1
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /home/richard/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p0 :002 > ^C

Any idea why this might be? I'm following the rails convention as far as I'm aware: I made a User model to correspond with a table called users already in the db, whose primary key is an integer field called id.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Richard Stokes
  • 3,532
  • 7
  • 41
  • 57

2 Answers2

4

I had similar problem with MySQL after upgrading to rails 3.2.2.

This is what I did to fix it.

rvm gem install mysql2
In GemFile remove old MySQL gem and add - gem ‘mysql2' 
bundle install
bundle update activerecord-mysql2-adapter
bundle update mysql2

then restart your rails server.

Majoris
  • 2,963
  • 6
  • 47
  • 81
4

Just had the same issue. Narrowed it to being a MySQL related issue since it was happening only in staging environment (MySQL) and not in the developpment one (sqlite3). Fixed it by upgrading the mysql2 from version 0.2.7 to 0.3.11.

Ecco
  • 1,323
  • 1
  • 11
  • 15
  • This worked for me. Edited Gemfile to remove the version '< 0.3'. Then called `bundle update mysql2` and recreated the database with `rake db:drop` followed by `rake --trace db:setup`. – peterept Mar 07 '12 at 21:56
  • I'm using 0.3.11 in a non-Rails app and am still having this issue. – Ben Hamill Feb 26 '13 at 22:06
  • 1
    @BenHamill, I am also having this problem with 0.3.11, non-Rails. Solved it by removing activerecord-mysql2-adapter from my gemspec. – Isaac Betesh Jul 10 '13 at 21:18