Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBI Row / delegate behavior between ruby 1.8.7 and 2.1

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?

like image 431
Rajat Avatar asked Feb 12 '23 04:02

Rajat


1 Answers

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/
like image 169
jsc Avatar answered Mar 12 '23 10:03

jsc