Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 Mongoid嵌入\u许多文档在重新加载后不会被删除_Ruby On Rails_Mongodb_Mongoid_Ruby On Rails 4_Mongoid3 - Fatal编程技术网

Ruby on rails Mongoid嵌入\u许多文档在重新加载后不会被删除

Ruby on rails Mongoid嵌入\u许多文档在重新加载后不会被删除,ruby-on-rails,mongodb,mongoid,ruby-on-rails-4,mongoid3,Ruby On Rails,Mongodb,Mongoid,Ruby On Rails 4,Mongoid3,我有以下两种型号: class Customer include Mongoid::Document include Mongoid::Timestamps embeds_many :locks, class_name: "Lock" accepts_nested_attributes_for :locks, allow_destroy: true field :name, type: String validates :name, presence: t

我有以下两种型号:

class Customer
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :locks, class_name: "Lock"
  accepts_nested_attributes_for :locks, allow_destroy: true

  field :name, type: String


  validates :name,
    presence: true

  belongs_to :list
end

因此,当我尝试删除锁时,锁会从子集合中移除,但在重新加载客户后,锁仍在那里

locks = customer.locks.where({ some conditions})

locks.each do |l|
  l.unlock!
end

customer.save
where条件肯定会返回正确的对象

谁能帮帮我,告诉我我做错了什么

更新:

这也不起作用

customer.locks = []
customer.save
customer.reload
好吧,让我们试试

首先,删除此块

 def unlock!
    self.destroy
 end
然后,替换

locks = customer.locks.where({ some conditions})
locks.each do |l|
      l.unlock!
    end

如果仍然不起作用,请在上面一行之后再添加一行

customer.locks.save
 customer.locks.where({ some conditions}).delete_all
customer.locks.save