我在索引页面中有一个student_names的列表。如果我删除了一个学生,将会破坏Mongo数据库中的整个文档。
但是如果我从索引页中删除它,它应该从列表中删除,但不想从Mongo数据库中删除。我不想破坏数据库中的任何数据!
I know the other ways to achieve它,但我想知道在mongoid中,我们是否需要包含一个额外的Mongoid模块来支持它。他们的任何特性在mongoid中都可用吗!
对于ex:
include Mongoid::Document
include Mongoid::Timestamps发布于 2014-03-20 02:13:37
没有,Mongoid为你提供了任何其他数据库都会提供的简单的CRUD功能。为了隐藏数据,您需要将属性设置为hidden,然后不在视图上显示隐藏的记录。
发布于 2014-03-21 02:36:39
是的,有
include Mongoid::Paranoia但这个问题不会在很长一段时间内得到支持。检查mongoid docs
因此,如果你计划升级到mongoid 4,不要依赖于此。
发布于 2014-03-21 10:28:16
如果你打算用最多的mongoid 3来做这件事,你可以使用Mongoid::Paranoia。Paranoia对删除的文档使用deleted_at属性,以将它们与其他文档区分开来,并在默认情况下对deleted_at: nil应用作用域。
include Mongoid::Paranoia
document.delete # Deleting it from index but keep it in DB
document.delete! # Delete it from collection permanently
document.destroy # Sets the deleted_at field, firing callbacks.
document.destroy! # Permanently deletes the document, firing callbacks.
document.restore # Brings the "deleted" document back to life.
collection_class.deleted # To show all deleted documents (marked as deleted)如果你计划和mongoid4一起移动,你可以试试https://github.com/simi/mongoid-paranoia,它对mongoid4也做同样的事情。
https://stackoverflow.com/questions/22504823
复制相似问题