Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4701

Allow writing fields omitted in #only

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: