Method: ActiveModelSerializers::Model.attributes
- Defined in:
- lib/active_model_serializers/model.rb
.attributes(names) ⇒ Object
For now, the Model only supports the notion of ‘attributes’. In the tests, there is a special Model that also supports ‘associations’. This is important so that we can add accessors for values that should not appear in the attributes hash when modeling associations. It is not yet clear if it makes sense for a PORO to have associations outside of the tests.
Easily declare instance attributes with setters and getters for each.
To initialize an instance, all attributes must have setters. However, the hash returned by attributes instance method will ALWAYS be the value of the initial attributes, regardless of what accessors are defined. The only way to change the change the attributes after initialization is to mutate the attributes directly. Accessor methods do NOT mutate the attributes. (This is a bug).
41 42 43 44 45 46 47 |
# File 'lib/active_model_serializers/model.rb', line 41 def self.attributes(*names) self.attribute_names |= names.map(&:to_sym) # Silence redefinition of methods warnings ActiveModelSerializers.silence_warnings do attr_accessor(*names) end end |