MongoMapper 嵌入式文档

发布于 2024-09-10 15:46:24 字数 847 浏览 7 评论 0原文

我将产品作为类别类中的嵌入文档,如下所示:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2024-09-17 15:46:24

好吧,首先要尝试的是你有:

many :products

...但是然后你尝试使用 category.Products.each 访问它

绝对保持你的命名一致,我建议使用 ruby​​ 约定(下划线,不是驼峰式大小写,当然也不是非类的大写驼峰式大小写)。

所以,也许:

class Category
  include MongoMapper::Document
  key :name, String
  many :products
end

class Product
  include MongoMapper::EmbeddedDocument
  key :name, String
end


categories = Category.all
categories.each do |category|
  puts category.name
  category.products.each do |product|
    puts "  " + product.name
  end
end

Well, first thing to try is that you have:

many :products

...but then you try to access it with category.Products.each

Definitely 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:

class Category
  include MongoMapper::Document
  key :name, String
  many :products
end

class Product
  include MongoMapper::EmbeddedDocument
  key :name, String
end


categories = Category.all
categories.each do |category|
  puts category.name
  category.products.each do |product|
    puts "  " + product.name
  end
end
智商已欠费 2024-09-17 15:46:24

您返回的对象就像哈希一样。为了访问名称,您需要使用product["Name"]category["Name"]

例如

irb(main):007:0> oh.baz
NoMethodError: undefined method `baz' for {"foobar"=>"baz"}:BSON::OrderedHash
    from (irb):7
irb(main):008:0> oh[:foobar]
=> "baz"

The object you're getting back acts like a hash. In order to access the name you need to use product["Name"] or category["Name"].

e.g.

irb(main):007:0> oh.baz
NoMethodError: undefined method `baz' for {"foobar"=>"baz"}:BSON::OrderedHash
    from (irb):7
irb(main):008:0> oh[:foobar]
=> "baz"
一梦浮鱼 2024-09-17 15:46:24

在你的控制器

@categories = Category.all

视图中

<% @categories.products.each do |product| %>
  <%= product.Name %> <br/>
<% end %>

In your controller

@categories = Category.all

View

<% @categories.products.each do |product| %>
  <%= product.Name %> <br/>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文