【发布时间】:2011-11-17 16:41:07
【问题描述】:
我有以下用户模型,嵌入类别模型,
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 命令。
需要帮助,谢谢:)
【问题讨论】:
标签: ruby-on-rails-3 mongodb mongoid