Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Mongoid中创建/更新链接文档?_Ruby_Mongodb_Mongoid - Fatal编程技术网

Ruby 如何在Mongoid中创建/更新链接文档?

Ruby 如何在Mongoid中创建/更新链接文档?,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,我有2个链接文档,我需要更新其中一个 class User include Mongoid::Document field :name has_one :resume end class Resume include Mongoid::Document field :title belongs_to :user end #............... new_resume = Resume.new({:title => "my title"}) if cur

我有2个链接文档,我需要更新其中一个

class User
  include Mongoid::Document
  field :name
  has_one :resume
end

class Resume
  include Mongoid::Document
  field :title
  belongs_to :user
end

#...............

new_resume = Resume.new({:title => "my title"})
if current_user.resume && current_user.resume.any? #if resume exists then update it
  current_user.resume.update_attribute(:resume, new_resume)
else #otherwise create it
  current_user.resume.create(:resume, new_resume) 
它不起作用,因为在第二种情况下,
current\u user.resume==nil


如何为当前用户更新/创建
恢复

直接传递散列,而不是mongo对象,即类似于当前用户.Resume.create({:title=>“mytitle”})当前用户.Resume为零,因此它将抛出一个异常。抱歉,Grienders它
有一个
因此语法将不同尝试此
create\u resume
当前用户。create\u resume({:title=>“mytitle})
Hmm,方法是否存在。创建简历?最好在文档中检查,即使是activerecord也遵循标准惯例,并且mongoid在这里有一个关联链检查,以便访问
1-1
参考上面的
1-n
表中的最后一个操作。您可以检查
activerecord
docum由于方法标准取自于
1-1
关系,因此