Consider the following code:

require "mysql"
db = Mysql.connect('localhost', 'root', '', 'test')

items = db.query('SELECT * FROM items')
tags = db.query('SELECT * FROM tags')

items.each_hash do |item|
   puts item["title"]
   tags.each_hash do |tag|
      puts tag["name"]
   end
end

puts tags.num_rows
puts items.num_rows

----
If the table "items" would contain:
id | title
1  | item01
2  | item02
3  | item03

and the table "tags" would contain:
id | name
1  | tag01
2  | tag02

The I would expect the output to be:
----expected output---
item01
tag01
tag02
item02
tag01
tag02
item03
tag01
tag02
2
3

However the output is as follows:
----actual output---
item01
tag01
tag02
item02
item03
2
3

--
--
Note how in the actual output the inner itterator isn't executed... and
yet as you can see from the "2, 3" in the end (which is from "puts
tags.num_rows" and "puts items.num_rows") the variable "tags" does
contain multiple elements.

Can anyone explain my why this output is behaving as it does? Could you
suggest a sensible way to get to the expected output?

Thanks in advance,
Andy

-- 
Posted via http://www.ruby-forum.com/.