小编use*_*199的帖子

如何在没有包含embeds_many关联的父文档的情况下允许持久化?

我有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 …

ruby-on-rails mongodb mongoid

3
推荐指数
1
解决办法
1687
查看次数

标签 统计

mongodb ×1

mongoid ×1

ruby-on-rails ×1