I execute the following code in ruby 1.8.7 to read rows from my database:
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
From the output it is clear that info[0].class is DBI::Row. This code works perfectly when executed with ruby 1.8.7 (rails 3.2.17)
When I try executing it in ruby 2.1.5 / rails 3.2.17, it gives the following error:
/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>'
The file /home/rjain/mac/query.rb is pasted above. I want to understand whats the difference between ruby 2.1 and 1.8 that causes this issue. What is the fix for this problem?
Ran into the same issue, tracked down the problem.
Find lib/dbi/row.rb in the gem directory. Line 212 or thereabouts should read
if RUBY_VERSION =~ /^1\.9/
Edit it to be
if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/
This too, was a problem we had with code written with Ruby 1.8x , when run in 2.1.0 it coughed up
/usr/lib/ruby/2.1.0/delegate.rb:392:in __getobj__': not delegated (ArgumentError)
from /usr/lib/ruby/2.1.0/delegate.rb:341:in
block in delegating_block'
Once we found jsc comment above, we altered the file row.rb in the dbi gem folder. Again, looking for the part of:
if RUBY_VERSION =~ /^1\.9/
and update it to
if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/
After that, the app ran without a problem.
来源:https://stackoverflow.com/questions/27873121/dbi-row-delegate-behavior-between-ruby-1-8-7-and-2-1