宝石2.0。与ActiveRecord 4.1.2的兼容性问题

时间:2015-03-31 11:50:32

标签: mysql ruby-on-rails ruby activerecord rubygems

我正在尝试使用mysql数据库并从该特定数据库获取数据,但它正在上升错误..,修复它plz

代码是:

require 'rubygems'
#require 'mysql'
require 'active_record'


ActiveRecord::Base.establish_connection(
    {
    :adapter => 'mysql',
    :host => 'localhost',
    :database => 'spyware',
    :user => 'root',
    :password => 'sqlpass'
    }
)

class Definition<ActiveRecord::Base
end

defni = Definition.Find(:all)

defni.each do |row|

    print "\n\n".row.name
end

我得到的错误是::

ruby dbconnect.rb
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2/lib/active_record/connection_adapters/connection_specification.rb:190:in `rescue in spec': Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2/lib/active_record/connection_adapters/connection_specification.rb:187:in `spec'
    from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.1.2/lib/active_record/connection_handling.rb:50:in `establish_connection'
    from dbconnect.rb:6:in `<main>'
>Exit code: 1

我使用的是2.0.14的gem版本,而mysql的版本是2.8.1 x86

1 个答案:

答案 0 :(得分:0)

Rails已经为您提供了答案:将gem 'mysql'添加到您的Gemfile中(并确保其版本达到ActiveRecord所需的最低要求)。 阅读错误日志是个好主意! ;)