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?