使用Mongoid将整个模型设为只读

问题描述:

我看到Mongoid支持只读属性.有没有办法将整个文档或整个集合/模型类标记为只读?

I see that Mongoid supports read-only attributes. Is there a way to mark an entire document, or an entire collection / model class as read-only?

您可以访问fields class属性,并使用attr_readonly设置哈希键.例如:

You can access the fields class attribute and splat the hash's keys against attr_readonly. For example:

class Model
  include Mongoid::Document

  attr_readonly *fields.keys
end

请注意,fields.keys将包括_id_type.