为什么使用变量时mongoid不允许创建新的嵌入式文档? [英] Why is mongoid not allowing for the creation of a new embedded document when using a variable?

查看:76
本文介绍了为什么使用变量时mongoid不允许创建新的嵌入式文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在删除嵌入文档后添加新的嵌入文档.但是当我使用时出现错误:

I am trying to add a new embedded document after deleting the embedded document. But I get an error when I use:

代码

u = User.last
u.classes.destroy_all
u.classes.create(:name => "Philsophy")
=> NoMethodError: undefined method `create' for []:Array

但是,如果我不使用变量,它将起作用.有人可以向我解释一下吗?我需要使用嵌套变量版本来遍历数百个项目的结果集.

BUT, If I don't use a variable it WORKS. Can someone please explain this to me? I need to use the nested variable version to loop through a result set of hundreds of items.

User.last.classes.destroy_all
User.last.classes.create(:name => "Philsophy")
=> #<Class _id: philosophy, name: "Philosophy"> 

Ruby on Rails 3.0.3
Mongoid 2.0.0.beta.20(在这里别无选择)

Ruby on Rails 3.0.3
Mongoid 2.0.0.beta.20 (Don't have a choice here)

谢谢

推荐答案

我不确定您的模型在这里到底是什么,但是我只能重现以下错误:

I am not sure exactly what your models are here but I was only able to reproduce the error you are getting with something like this:

class User
  include Mongoid::Document

  embeds_many :classes
end

class Class
  include Mongoid::Document
  field :name

  embedded_in :user, :inverse_of => :classes
end

如果这是您的全部,那么问题是您正在从Ruby标准库中打开Class类,并将其有效地扩展为Mongoid模型.我想那不是故意的.在这种情况下,您有几个选择,您可以将Class类重命名为不会与标准库冲突的名称,例如,可以很好地工作:

If that is what you have then the problem is that you are opening the Class class from the Ruby standard library and effectively extending it to be a Mongoid model. I guess that is not intentional. You have a couple of options in that case, you could just rename your Class class to something that isn't going to clash with the standard library, for example this works fine:

class User
  include Mongoid::Document

  embeds_many :courses
end

class Course
  include Mongoid::Document
  field :name

  embedded_in :user, :inverse_of => :courses
end

或者如果您确实想要将类称为类",则可以将模型放在命名空间中:

Or you could put your models in a namespace if you really want the call your class 'Class':

module Models
  class User
    include Mongoid::Document

    embeds_many :classes, :class_name => 'Models::Class'
  end

  class Class
    include Mongoid::Document
    field :name

    embedded_in :user, :inverse_of => :classes, :class_name => 'Models::User'
  end
end

如果执行此操作,则必须通过指定class_name来帮助Mongoid在每种关系中找到目标类.因此,最容易重命名该类.

If you do this then you will have to give Mongoid some help to find the target classes in each of your relations by specifying the class_name. So probably easiest to rename the class.

关于为什么仅在使用像第一个示例那样的变量时才出现问题的原因,我怀疑这是因为destroy_all方法使类集合处于断开状态(名称冲突的副作用).在第二个示例中,第二行正在加载User模型的新实例,这样就不会出现问题.

As to why the problem happens only when you use a variable like in your first example, I suspect it is because the destroy_all method leaves the classes collection in a broken state (a side-effect of the name clash). In your second example the second line is loading a fresh instance of the User model so that problem doesn't arise.

这篇关于为什么使用变量时mongoid不允许创建新的嵌入式文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆