Ho seguente modello d'uso, incorpora il modello di Categoria,MongoDB - creare un nuovo documento in un array integrato
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
La mia domanda è, ho appena scoperto che se io uso il codice:
me = User.where("some conditions")
me.categories << Category.new(:name => "party")
tutto funziona bene, ma se uso il metodo .create:
me = User.where("some conditions")
me.categories << Category.create(:name => "party")
mi metterò un'eccezione:
undefined method `new?' for nil:NilClass
Qualcuno sa perché? E da mongoid.org http://mongoid.org/docs/persistence/standard.html, ho potuto vedere che .new e .create generano effettivamente lo stesso comando mongo.
Ha bisogno di aiuto, grazie :)
totalmente capire, molto chiaro. – larryzhao