-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: Attributes, Persistence
Currently, referencing a field which is omitted from #only produces ActiveModel::MissingAttributeError. However, this field can be written to:
person = Person.where(username: 'Dev').only(:_id, :username).first
person.age = 42
person.save!
However, this write is not persisted:
person.reload.age # => 100
... and reading person.age after writing it still yields ActiveModel::MissingAttributeError.
This ticket is to investigate whether it is possible to write to attributes omitted in #only list, and when doing so change the list of loaded attributes to include attributes written to.
Note that Mongoid provides 4 ways to write data into fields:
Generated field accessor (model.field = 'foo')
write_attribute (model.write_attribute(:field, 'foo'))
[]= (model['field'] = 'foo'), this is an alias of write_attribute
set (model.set(field: 'foo')), per MONGOID-5059
Because the set method involves a model instance attribute write (write_attribute), a solution for this ticket would likely enable writes via all four possible mechanisms.
- is related to
-
MONGOID-5474 Raise Mongoid::Errors::ReadonlyDocument when saving readonly documents
- Closed
- related to
-
MONGOID-5059 `set` nilifies attributes excluded via `only`
- Closed
-
MONGOID-2681 Using #only with embedded documents sets omitted fields to default values
- Closed