gpt4 book ai didi

ruby-on-rails - 如何从 Mongoid 模型中删除属性,即不仅仅是取消它们的值

转载 作者:行者123 更新时间:2023-12-01 16:51:21 24 4
gpt4 key购买 nike

我正在尝试 Mongoid 中的多态关联

class Group
include Mongoid::Document
belongs_to :groupable, polymorphic: true
end

class Album
include Mongoid::Document
has_many :groups, as: groupable
end

然后我决定不这样做。所以我删除了上面所有的belongs_to和has_many行。然而在控制台中,每当我获得我尝试过的组记录时,它仍然具有此“groupable_type”属性。我知道remove_attribute会使属性无效,但不会删除它(听起来有点像JavaScript)。我怎样才能真正从 Mongoid 的数据库中删除这个属性?

最佳答案

你可以这样做:

Group.collection.update({},
{'$unset' => {:groupable_type => 1}},
:multi => true)

关于ruby-on-rails - 如何从 Mongoid 模型中删除属性,即不仅仅是取消它们的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9144299/

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