I have products as an embedded document inside the category class as shown below:
require 'mongo_mapper'
class Category
include MongoMapper::Document
key :Name, String
key :NumberOfProducts, Integer
many :products
end
and here is the Product class:
require 'mongo_mapper'
class Product
include MongoMapper::EmbeddedDocument
key :Name, String
end
I am using the following code to display the Products but it says no method “Name” found.
require 'rubygems'
require 'mongo'
require 'mongo_mapper'
require 'category'
require 'product'
include Mongo
MongoMapper.database = 'Northwind'
categories = Category.all()
categories.each{|category| puts category.Name
unless category.Products.nil?
category.Products.each{|product| puts product.Name}
end
}
here is the error:
undefined method `Name' for {"Name"=>"Amiga"}:BSON::OrderedHash (NoMethodError)
Well, first thing to try is that you have:
…but then you try to access it with
category.Products.eachDefinitely keep your naming consistent, and I’d recommend using ruby conventions (underscored, not camel cased, and certainly not capitalized camel case for non-classes).
So, maybe: