I have read the answers here on batch insert e.g. batch insert mongoid
I have 2 collections:
class User
include UpdateUserOrCreate
include Mongoid::Document
include Mongoid::Timestamps
has_many :messages, class_name: "Api::V1::Message", autosave: true, validate: false
has_many :message_export_requests, class_name: "Api::V1::MessageExportRequest", autosave: true, validate: false
end
class Message
include Mongoid::Document
include Mongoid::Timestamps
belongs_to :user, class_name: "Api::V1::User", autosave: true, foreign_key: :user_id
end
I have a document array :
batch = [{name: "dsfdf" },{name: "dfsdfh"}]
I am trying to do:
user.messages.collection.insert(batch)
But the result is that the Message documents are saved with user_id = nil.
How can I batch save the documents in the array through the relation making sure that the foreign key is set??