简体   繁体   中英

Making an entire model read-only with 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?

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

Note that, fields.keys will include _id and _type .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM