Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
Leopd Avatar asked Jan 29 '13 00:01

Leopd


1 Answers

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.

like image 94
mikeycgto Avatar answered Sep 18 '22 13:09

mikeycgto