I have been fiddling with Mongo, but can't get this simple example to work. I'm simply trying to retrieve all documents in a collection:
require 'mongoid'
# configuration ...
class Category
include Mongoid::Document
field :name, type: String
end
Category.each do |test|
puts test.inspect
end
I get the error: undefined method 'each' for Category:Class (NoMethodError).
Connection to the database is well established, and a collection named categories
contains a few documents.
Category
indeed doesn't have a method each
because it's a model class, not a collection. It has, however, several methods that do return collection-like objects. One of them is all
. So the code should look like this:
Category.all.each do |test|
puts test.inspect
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With