Tôi đã theo mô hình dùng, nhúng mô hình loại,MongoDB - tạo ra một tài liệu mới trong một mảng nhúng
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
Câu hỏi của tôi là, tôi chỉ thấy rằng nếu tôi sử dụng mã:
me = User.where("some conditions")
me.categories << Category.new(:name => "party")
tất cả mọi thứ hoạt động tốt, nhưng nếu tôi sử dụng phương pháp .Create:
me = User.where("some conditions")
me.categories << Category.create(:name => "party")
tôi sẽ nhận được một ngoại lệ:
undefined method `new?' for nil:NilClass
Có ai biết tại sao không? Và từ mongoid.org http://mongoid.org/docs/persistence/standard.html, tôi có thể thấy rằng .new và .create thực sự tạo ra cùng một lệnh mongo.
Cần trợ giúp, cảm ơn :)
hoàn toàn hiểu, rất rõ ràng. – larryzhao