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