モジュール: Mongoid::Attributes::Readonly::Classメソッドs

定義:
lib/mongoid/attributes/readonly.rb

インスタンス メソッドの概要を折りたたむ

インスタンス メソッドの詳細

#attr_readonly (*names) =オブジェクト

注:

親クラスに読み取り専用属性が含まれており、次が

属性を読み取り専用として定義します。 これにより、ドキュメントが新しい場合、または作成中のときにのみ 属性の値が設定されるようになります。 その他の場合、 フィールドの書込みは無視されます。ただし、#remove_attribute と #update_attribute ではエラーが発生します。

子クラスによって継承された場合、子クラスは作成時に親の読み取り専用属性を継承します。 親を更新しても、 ワード後に子クラスに伝播することはありません。

例:

フィールドを読み取り専用としてフラグします。

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

パラメーター:

  • *names (Symbol...)

    フィールドの名前。



70
71
72
73
74
75
# ファイル 'lib/mongoid/attributes/readonly.rb' 行70

デフォルト attr_readonly(*name)
  自己.readonly_attributes = 自己.readonly_attributes.dup
  name. 行う |name|
    自己.readonly_attributes < database_field_name(name)
  end
end