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

Allow writing fields omitted in #only

    XMLWordPrintable

Details

    • New Feature
    • Status: Backlog
    • Minor - P4
    • Resolution: Unresolved
    • None
    • None
    • Attributes, Persistence

    Description

      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.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated: