我将产品作为类别类中的嵌入式文档,如下所示:
require 'mongo_mapper'
class Category
include MongoMapper::Document
key :Name, String
key :NumberOfProducts, Integer
many :products
end
这是产品类:
require 'mongo_mapper'
class Product
include MongoMapper::EmbeddedDocument
key :Name, String
end
我正在使用以下代码来显示产品,但它说没有找到方法“名称”。
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
}
这是错误:
undefined method `Name' for {"Name"=>"Amiga"}:BSON::OrderedHash (NoMethodError)