Mongodb - create a new document in an inline array

I have the following user model, which includes the category model,

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

My question is: I just found that if I use the code:

me = User.where("some conditions")
me.categories << Category.new(:name => "party")

everything works fine, but if I use the .create method:

me = User.where("some conditions")
me.categories << Category.create(:name => "party")

I will get an exception:

undefined method `new?' for nil:NilClass

Does anyone know why? And from mongoid.org http://mongoid.org/docs/persistence/standard.html I could see that .new and .create actually generate the same mongo command.

Need help, thanks :)

+5
source share
1 answer

. ( ), . .

, , . , . ?

new ​​ < <

Category.create(:name => "party")
>>NoMethodError: undefined method `new?' for nil:NilClass

 c = Category.new(:name => "party")
 c.save
 >>NoMethodError: undefined method `new?' for nil:NilClass

,

+10

All Articles