当前位置 : 主页 > 编程语言 > ruby >

ruby-on-rails-3.2 – DBI在ruby 1.8.7和2.1之间的行/委托行为

来源:互联网 收集:自由互联 发布时间:2021-06-23
我在 ruby 1.8.7中执行以下代码来从我的数据库中读取行: require 'dbi'db_conn_handle = DBI.connect("DBI:Mysql:host=localhost;database=mydb;port=3306", "root")sth = db_conn_handle.prepare("select accounts.id, accounts.name
我在 ruby 1.8.7中执行以下代码来从我的数据库中读取行:

require 'dbi'
db_conn_handle = DBI.connect("DBI:Mysql:host=localhost;database=mydb;port=3306", "root")
sth = db_conn_handle.prepare("select accounts.id, accounts.name from accounts;")
sth.execute
info = sth.to_a
puts "Info: #{info[0].class}"
info.each do |x, y|
  puts "#{x} ... #{y}"
end

从输出中可以清楚地看出,info [0] .class是DBI :: Row.使用ruby 1.8.7(rails 3.2.17)执行此代码时效果很好

当我尝试在ruby 2.1.5 / rails 3.2.17中执行它时,它会出现以下错误:

/home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:392:in `__getobj__': not delegated (ArgumentError)
    from /home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:341:in `block in delegating_block'
    from /home/rjain/mac/query.rb:7:in `each'
    from /home/rjain/mac/query.rb:7:in `<top (required)>'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `eval'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `<top (required)>'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `require'
    from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

文件/home/rjain/mac/query.rb粘贴在上面.我想了解ruby 2.1和1.8之间导致此问题的区别.这个问题的解决方法是什么?

陷入同样的​​问题,追查问题.

在gem目录中找到lib / dbi / row.rb. 212行或左右应阅读

if RUBY_VERSION =~ /^1\.9/

编辑它

if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/
网友评论