Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.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 on rails 保存后,使用Mongo更新子对象上的父模型_Ruby On Rails_Ruby_Mongodb_Model_Mongoid - Fatal编程技术网

Ruby on rails 保存后,使用Mongo更新子对象上的父模型

Ruby on rails 保存后,使用Mongo更新子对象上的父模型,ruby-on-rails,ruby,mongodb,model,mongoid,Ruby On Rails,Ruby,Mongodb,Model,Mongoid,一个用户有许多帖子,一篇帖子属于一个用户。两者都有一个位置。我想存储每个帖子的位置,并用最新的帖子位置更新用户位置。你是不是想留个路走?我对Mongo DB非常缺乏经验 后期模型 class Post include Mongoid::Document belongs_to :user after_save :update_user_location field :location, :type => String def update_user_location

一个用户有许多帖子,一篇帖子属于一个用户。两者都有一个位置。我想存储每个帖子的位置,并用最新的帖子位置更新用户位置。你是不是想留个路走?我对Mongo DB非常缺乏经验

后期模型

class Post
  include Mongoid::Document
  belongs_to :user

  after_save :update_user_location
  field :location, :type => String

  def update_user_location
     #update user's location
  end
end
class User
  include Mongoid::Document

  has_many :posts
  field :location, :type => String
end
用户模型

class Post
  include Mongoid::Document
  belongs_to :user

  after_save :update_user_location
  field :location, :type => String

  def update_user_location
     #update user's location
  end
end
class User
  include Mongoid::Document

  has_many :posts
  field :location, :type => String
end

(请不要说只是获取最新的发布位置,我需要单独存储它,因为有一个原因…thx)

这应该可以,你尝试过什么吗?顺便说一句,这确实是一个特定于Mongoid的问题,与MongoDB本身无关

def update_user_location
    self.user.location = self.location
    self.user.save
end

这个应该可以,你试过什么吗?顺便说一句,这确实是一个特定于Mongoid的问题,与MongoDB本身无关

def update_user_location
    self.user.location = self.location
    self.user.save
end
这应该起作用:

def update_user_location
    self._parent.location = self.location
    self._parent.save
end
但是我想我已经注意到一个bug,如果你的父模型(用户)有一些before_save函数,self._parent.save是无效的。。。我已经遇到过两次这种情况,找不到任何解释

Alex

这应该可以:

def update_user_location
    self._parent.location = self.location
    self._parent.save
end
但是我想我已经注意到一个bug,如果你的父模型(用户)有一些before_save函数,self._parent.save是无效的。。。我已经遇到过两次这种情况,找不到任何解释


亚历克斯,这正是我的问题。我试着在验证前保存到验证后,这对我很有效。奇怪,我以为只有我。。。我还是一个新来的蒙哥亚人,还有一些古怪的行为,但这是蒙哥德人而不是蒙哥德人。我相信亚历克斯,这正是我的问题。我试着在验证前保存到验证后,这对我很有效。奇怪,我以为只有我。。。我对mongoyeah还是个新手,仍然有一些古怪的行为,但我相信这是mongoid而不是mongodb