使用Mongoid将整个模型设为只读 [英] Making an entire model read-only with Mongoid

查看:61
本文介绍了使用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.

这篇关于使用Mongoid将整个模型设为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆