This is probably simple, but I have spent way too much time trying to figure it out, and am sure someone here will know, so here goes. Please be patient.
Bottom line is that I’ve got some data that I can’t figure out how to loop over.
#Get the data from mongomapper map_reduce
@urls = DistinctUrls.build.find()
puts @urls.count
3
puts @urls.to_json
[{"_id":"http://msn.com","value":3.0},{"_id":"http://yahoo.com","value":12.0},{"_id":"http://google.com","value":2.0}]
@urls.each do |entry|
puts "Here I am" # Never gets printed, not sure why.
puts "url" + entry['_id']
end
What I don’t understand is that if I have a count of 3, why it won’t enter the loop?
I’m not sure if the mongomapper or map_reduce details matter. I’m putting them here just in case. If it makes sense, I can add the details of the map/reduce if needed.
Thanks for your help.
First you wrote
@urlsthen@url. I think only one of them is correct.Update: As the documentation says you can iterate over the cursor with
eachbut after the full iteration it will be closed. Maybe this is your case that you’ve already iterated over it once. Probably theto_jsondid it.You can check whether the cursor is closed or not with the following statement:
Check this before the iterating part.