Undefined method `each' for #<Mysql:0x3874b20>

Running on XP, Rails v1.1.6, Ruby 1.8.4, mysql 5.0.24, WEBrick 1.3.1

just doing a basic ‘scaffold’ against a table in mysql, I originally got
the “undefined method ‘each’” error at all times then saw something
advising to uninstall the gem mysql package, which I did.

then it seemed to work! - but only the first ‘list’ call to the website
returns the data from the table - reloading the identical page
thereafter generates this error, so it is not at all stable. Restarting
WEBrick again works first call only

Any advice appreciated

[2006-08-13 14:00:19] INFO WEBrick 1.3.1
[2006-08-13 14:00:19] INFO ruby 1.8.4 (2006-04-14) [i386-mswin32]
[2006-08-13 14:00:19] INFO WEBrick::HTTPServer#start: pid=4040
port=3000
127.0.0.1 - - [13/Aug/2006:14:00:41 Eastern Daylight Time] “GET
/hello/list HTTP/1.1” 200 1865- -> /hello/list
127.0.0.1 - - [13/Aug/2006:14:00:44 Eastern Daylight Time] “GET
/hello/list HTTP/1.1” 500 8842- -> /hello/list

trace from the web page
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:292:in
columns' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:696:incolumns’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/calculations.rb:216:in
column_for' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/calculations.rb:138:incalculate’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/calculations.rb:66:in
count' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/pagination.rb:172:incount_collection_for_pagination’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/pagination.rb:196:in
paginator_and_collection_for' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/pagination.rb:129:inpaginate’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/scaffolding.rb:109:in
list' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:inperform_action_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in
perform_action_without_benchmark' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
c:/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in
perform_action' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:inprocess_without_filters’
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in
process_without_session_management_support' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:inprocess’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in
dispatch' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:inhandle_dispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in
service' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' c:/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in start' c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:indispatch’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:inrequire’
c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:inrequire’
script/server:3

I’m having EXACTLY the same problem however I’m running Apache 2. The
first call works however if I refresh the page, it no longer works (or
any other call to the database) and I get:

undefined method `each’ for #Mysql:0x342fca4

I also run MSSQL server on this machine and if I point the database.yml
to that instead (took a lot of setting up but finally got it working),
it works perfectly. Which is odd…

Any help greatly appreciated.

Nick

Nick wrote:

I’m having EXACTLY the same problem however I’m running Apache 2. The
first call works however if I refresh the page, it no longer works (or
any other call to the database) and I get:

undefined method `each’ for #Mysql:0x342fca4

I also run MSSQL server on this machine and if I point the database.yml
to that instead (took a lot of setting up but finally got it working),
it works perfectly. Which is odd…

Any help greatly appreciated.

Nick

I had this exact problem on a development based windows machine. The
problem was my libmysql.dll was not in sync with the MySQL server that i
had running. (I used instantrails for the mysql server and just
downloaded the 5.0 release dll from MySQL.com)
So i suggest if you have upgraded your MySQL install make sure that you
have the mysql gem re compiled so that it is linked with the same mysql
libraries.

Hi and thanks for the very fast reply!

The funny thing is that I have only ever used MySQL v.5.0. I did however
use InstantRails at one point just to see what it was like. I didn’t
like it, uninstalled it and reinstalled MySQL v.5.0.

Another odd thing is that I was having problems making Rails work at all
with the DB… I read somewhere that all I needed to do was copy the
libmySQL.dll file to my Windows\System32 directory. Which I did. Worked.
However, then I started experiencing these problems described above!

Finally, sorry to sound like a fool, but what exactly do you mean by
“have the mysql gem re compiled” and how would I go about doing that?

With thanks again

Nick

Thanks for the reply but that wasn’t the problem in the end. I have
successfully found what the problem was!

Having rumaged around on the web for days, I came across someone
suggesting that I install a different mysql.so in the
“C:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt” folder. I did. Nothing
changed. While trying the two different files, I was constantly changing
the names to mysql.so_old or something like that. At one point BOTH were
named _old and _new… i.e. Ruby couldn’t find a SINGLE mysql.so.

And would you believe this actually worked!

It was the mysql.so file that was causing problems… Simply removing
the file made it all work again!

Very odd indeed!

Thanks for your help all the same!

Nick

Ok sorry i thought you were on a *nix type platform where the gem’s are
linked against your library’s. If you are on windows then just make sure
that you are using the proper libmysql.dll from your distribution.

Normally it is located in the bin directory of your mysql install.

Also i actually perfer to put the libmysql.dll in the directory where
the ruby.exe is located. that way i don’t have stuff cluttering up the
Windows system dir. Also scan your harddrive and see if there is more
instances of libmysql.dll that you might not know about that ruby is
picking up.

Just to add to my last post…

The major problem is that there does not appear to be ONE SINGLE
installation tutorial for Ruby/Apache/MySQL on Windows XP that actually
works. Everyone I spoke to has had to use a strange mix of numerous
different tutorials to finally get it working.

Naturally, I tried numerous different things to finally get mine working
and probaby one of them involved installing this stupid mysql.so file.

Anyway, fingers crossed, no more problems…

Nick

Schultz wrote:

Nick,
I think when you deleted the mysql.so file you actually put the mysql
gem into ruby only mode. So you will have alot slower times doing stuff
with the database. instead of using a native module. (Though i am not
100% sure on this though) And i know your pain. I currently have 3
seperate environments (Linux, Windows, MacOSX) that i do development in
and Windows one has been the hardest to setup.

Rob S.

hey man,

before i used to use the instant rails because it was so easy, but
recently i just installed MySQL v.5.0. with ruby 1.8 and i get this
error. and i am using webrick do you know what i need to do to make this
work ?

i would appreciate it if you could help me

thank you

Nick,
I think when you deleted the mysql.so file you actually put the mysql
gem into ruby only mode. So you will have alot slower times doing stuff
with the database. instead of using a native module. (Though i am not
100% sure on this though) And i know your pain. I currently have 3
seperate environments (Linux, Windows, MacOSX) that i do development in
and Windows one has been the hardest to setup.

Rob S.

Hi Nick,

I had the same problem - and you helped me very much! Renaming mysql.so
solves all , now Rails works fine.

Thank you

thomas

FYI:
This problem can be caused by an a mismatch between versions of the
mySQL gem and the version of mySQL. If you are using version 2.7.3 of
the mySQL driver, make sure you upgrade your mySQL installation to
5.0.27 from 5.0.24 - that can fix the problem (worked for me…)

  • John

Thomas Walter wrote:

Hi Nick,

I had the same problem - and you helped me very much! Renaming mysql.so
solves all , now Rails works fine.

Thank you

thomas

This error drives me crazy! For the last 3 hours I was trying to
unsuccessfully find out what is wrong with my script, I have pinpointed
it to this:

I am running two SELECT queries like this:

require ‘mysql’

 dbh = Mysql.connect("localhost", "aaa", "aaa", "aaa")

 #Query 1
 res = dbh.query("SELECT id FROM logs WHERE id='10'");
 puts "Number of rows returned: #{res.num_rows}"
 res.free

 #Query 2
 res = dbh.query("SELECT id FROM logs WHERE id='10'");
 puts "Number of rows returned: #{res.num_rows}"
 res.free

And here is what I’m getting:

For the first query everything is ok: Number of rows returned:0
For the 2nd: undefined method ‘num_rows’ for #Mysql:0x34eaaa18
(NoMethodError)

For some reason the second query does not return an object of class
Mysql::Result and I have no idea why. I even tried to dbh.close and
reopen - it does not help! The INSERTS are working fine, I tried several
insert statements, and they work without a glitch.

My set-up:

OS: Windows VISTA
MySQL GEM: 2.7.3
MySQL: 5.1.22
Ruby: 1.8.6

Your help will be extremely appreciated!

Vlad V. wrote:

For some reason the second query does not return an object of class
Mysql::Result and I have no idea why. I even tried to dbh.close and
reopen - it does not help! The INSERTS are working fine, I tried several
insert statements, and they work without a glitch.

I’ve managed to go around it, although I do not understand what caused
an issue. Any way, when I use the block notation, it works fine:

dbh.query(“SELECT id FROM logs WHERE id=‘10’”) { |res| puts “Number of
rows returned: #{res.num_rows}” }

Hope it helps someone, and if you can explain what causes that
behaviour, will be very good.