gpt4 book ai didi

ruby-on-rails - Mongoid pull_all 方法不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:19 25 4
gpt4 key购买 nike

我在 Mongoid 文档中有一组引用对象。使用 current_user.add_to_set(:whatever, @object.id) 效果很好,将 BSON ObjectId 元素添加到数组并保存 current_user

但是,当我尝试使用 pull_all 删除元素时,它不起作用。似乎什么都没发生:

current_user.pull_all(:whatever, BSON::ObjectId(@object.id))

知道我做错了什么吗?

谢谢!

最佳答案

您需要将要pull_all 的项目包装在一个数组中,我运行了一些类似的东西(在 Mongoid 2.0.2 中)并且它工作正常:

current_user.add_to_set(:whatever, @object.id) # worked exactly as you said, added the id to the `whatever` array
current_user.pull_all(:whatever, [@object.id]) # removed the id from the `whatever` array

这样,您还可以在一次操作中指定要删除的多个 ID,因此:

current_user.pull_all(:whatever, [@object1.id, @object2.id])

关于ruby-on-rails - Mongoid pull_all 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com