Alex Brickman Alex Brickman - 5 months ago 25
Ruby Question

ruby/sql syntax undefined method `query' for nil:NilClass (NoMethodError)

I keep getting the following error message:


.rb:215:in
block in collectData': undefined method
query' for nil:NilClass (NoMethodError)


This is my code:

databases = {
'aaa': Mysql2::Client.new(
host: '',
username: '',
password: '',
database: ''
),
'bbb': Mysql2::Client.new(
host: '',
username: '',
password: '',
database: ''
)
}

client1.query(" bla bla bla").each do |row|
if row['mmm'] == 99
# Look in next db
next_db = databases[row['db']]
row2 = next_db.query("SELECT...")


I take the name of the db from my first sql query,
client1.query
, for example
row['db']
=
"aaa"
or
"bbb"
.

For the test something like this also not working:

client1.query(" bla bla bla").each do |row|
if row['mmm'] == 99
# Look in next db
next_db = databases['aaa']
row2 = next_db.query("SELECT...")


And something like this works:

client1 = Mysql2::Client.new(
host: '',
username: '',
password: '',
database: ''
)

client2 = Mysql2::Client.new(
host: '',
username: '',
password: '',
database: ''
)

client1.query(" bla bla bla").each do |row|
if row['mmm'] == 99
# Look in next db
next_db = client2
row2 = next_db.query("SELECT ")


I can't seem to understand why my method is "undefined".

Please help,

Thanks !

Answer

try to change the hash of databases to:

databases = {
   'aaa' => Mysql2::Client.new(host: '', username: '', password: '', database: ''),
   'bbb' => Mysql2::Client.new(host: '',username: '', password: '', database: '')
 }