mysql - 无法激活 mysql2 (~> 0.3.6),已在 Rails 3.1 中激活 mysql2-0.3.2

标签 mysql ruby-on-rails rake bundler ruby-on-rails-3.1

我只是想获得在 3.1 下运行的 Rails 应用程序的最基本的基本 shell,当我运行 bundle exec rake db:migrate- 时出现了这个奇怪的错误

Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)

我在这里和其他地方读到的所有帖子都说我应该为 rails 3.1 使用更新的 mysql2 适配器,所以我有-

gem 'mysql2', '0.3.2'

在我的 gemfile 中。一些帖子建议使用-

gem 'mysql2', '~> 0.3'

但这让我犯了同样的错误。 gem 安装在-

/Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.3.2

有人建议我再次切换我的 gemfile 中的那一行,这次是-

gem 'mysql2', '< 0.3'

但是当我这样做时,运行 bundle install,然后尝试再次运行迁移,我得到-

An error has occurred, all later migrations canceled:
undefined method `rows' for nil:NilClass

我的完整迁移文件如下所示-

class CreatePlaces < ActiveRecord::Migration
  def change
    create_table :places do |t|
      t.string :title
      t.string :meta_description
      t.string :permalink, :limit => 60
      t.string :name, :limit => 60
      t.string :address
      t.string :state, :limit => 2
      t.string :region, :limit => 3
      t.float :latitude
      t.float :longitude
      t.text :description
      t.boolean :active, :default => true

      t.timestamps
    end
    add_index :places, [:permalink, :state, :region, :latitude, :longitude, :active], :name => 'places_index'
  end
end

运行该迁移的完整输出是-

==  CreatePlaces: migrating ===================================================
-- create_table(:places)
   -> 0.0925s
-- add_index(:places, [:permalink, :state, :region, :latitude, :longitude, :active], {:name=>"places_index"})
   -> 0.1097s
==  CreatePlaces: migrated (0.2023s) ==========================================

rake aborted!
An error has occurred, all later migrations canceled:

undefined method `rows' for nil:NilClass

没有以后的迁移,那是唯一的迁移,因为这是我刚开始尝试让 Rails 3.1 正常运行的应用程序。删除数据库并重新创建它让我回到同一个地方。

我可以从控制台访问 Places-

ruby-1.9.2-p180 :001 > Place
   (0.3ms)  SHOW TABLES
   (0.1ms)  SHOW TABLES
   (1.1ms)  describe `places`
 => Place(id: integer, title: string, meta_description: string, permalink: string, name: string, address: string, state: string, region: string, latitude: float, longitude: float, description: text, active: boolean, created_at: datetime, updated_at: datetime) 

但是当我实际尝试在 Places 上进行查找或任何操作时,我收到以下错误-

Place.find(:all)
ArgumentError: wrong number of arguments (3 for 2)
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:634:in `select'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/database_statements.rb:9:in `select_all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:470:in `find_by_sql'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation.rb:111:in `to_a'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:155:in `all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:105:in `find'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in `find'
    from (irb):2
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:45:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:8:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

有人有什么想法吗?我已经挖了大约 18 个小时,只是原地打转。

谢谢, --马克

最佳答案

Active Record 对 mysql2 的兼容版本有自己的要求。这是 line of code对于 Rails 3.1。您必须使用满足这些要求的 mysql2 版本。

Please install the mysql2 adapter: gem install activerecord-mysql2-adapter (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)

这是说 Rails 期望 mysql2 版本高于 0.3.6 且低于 0.4.0,但发现版本为 0.3.2。如果您更改 Gemfile 以请求此范围内的版本,那么 Active Record 应该会很高兴。也许

gem 'mysql2', '0.3.6'

不要忘记在更改 Gemfile 后更新您的包。

bundle update mysql2

关于mysql - 无法激活 mysql2 (~> 0.3.6),已在 Rails 3.1 中激活 mysql2-0.3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826036/

相关文章:

javascript - 在 Rails 应用程序中未找到来自 node_modules 的 .map 扩展名

ruby-on-rails - Rails rake 数据库 :seed not creating objects that belong_to another object?

ruby - Resque - 可以用来执行 Rake 任务吗?

ruby-on-rails - 如何使用 ruby​​ on rails 事件记录创建二进制索引?

ruby-on-rails - ActiveRecord - 如果没有 child 离开,则销毁 parent

python - Rake:Django 中的任务等价物

mysql - 为什么这个sql是正确的? (sql注入(inject))

mysql查询以矩阵形式拉取数据

java - 带有 jsp 和 mysql 的 ajax : java. sql.SQLException:用户 'root' @'localhost' 的访问被拒绝(使用密码:是)

php - 在 PHP 中更新 JSON 数组