gpt4 book ai didi

ruby-on-rails-3.2 - ruby 1.8.7 和 2.1 之间的 DBI 行/委托(delegate)行为

转载 作者:行者123 更新时间:2023-12-01 09:55:50 24 4
gpt4 key购买 nike

我在 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/

关于ruby-on-rails-3.2 - ruby 1.8.7 和 2.1 之间的 DBI 行/委托(delegate)行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27873121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com