5
我有以下的用户模型创建一个新文档,嵌入类型模型,MongoDB的 - 在一个嵌入式阵列
class User
include Mongoid::Document
include BCrypt
field :email, :type => String
field :password_hash, :type => String
field :password_salt, :type => String
embeds_many :categories
embeds_many :transactions
....
end
我的问题是,我才发现,如果我使用的代码:
me = User.where("some conditions")
me.categories << Category.new(:name => "party")
一切工作正常,但如果我用.create方法:
me = User.where("some conditions")
me.categories << Category.create(:name => "party")
我会得到一个异常:
undefined method `new?' for nil:NilClass
任何人都知道这是为什么?从mongoid.org http://mongoid.org/docs/persistence/standard.html,我可以看到.new和.create实际上会生成相同的mongo命令。
需要帮助,谢谢:)
完全理解,非常清晰。 – larryzhao