Module: Mongoid::Attributes::Readonly::ClassMethods

Defined in:
build/mongoid-7.3/lib/mongoid/attributes/readonly.rb

Instance Method Summary collapse

Instance Method Details

#attr_readonly(*names) ⇒ Object

Defines an attribute as readonly. This will ensure that the value for the attribute is only set when the document is new or we are creating. In other cases, the field write will be ignored with the exception of #remove_attribute and #update_attribute, where an error will get raised.

Examples:

Flag fields as readonly.

class Band
  include Mongoid::Document
  field :name, type: String
  field :genre, type: String
  attr_readonly :name, :genre
end

Parameters:

  • names (Array<Symbol>)

    The names of the fields.

Since:

  • 3.0.0



70
71
72
73
74
# File 'build/mongoid-7.3/lib/mongoid/attributes/readonly.rb', line 70

def attr_readonly(*names)
  names.each do |name|
    readonly_attributes << database_field_name(name)
  end
end