我有2个Mongoid模型,看起来像这样:
class User
include Mongoid::Document
field :name, type: String
embeds_many :jobs
end
class Job
include Mongoid::Document
field :title, type: String
embedded_in :user
end
Run Code Online (Sandbox Code Playgroud)
This allows me to do something like
user.jobs.create(title: 'Test Job')
Run Code Online (Sandbox Code Playgroud)
However, I'd like to be able to have some predefined jobs for a user to choose from, which would then be embedded in the user's document. Something like this:
Job.create(title: 'Predefined Job')
user.jobs << Job.first
Run Code Online (Sandbox Code Playgroud)
However, creating a job on it's own throws the following error
Cannot …