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

Document readonly attributes ignoring assignment

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Attributes
    • None
    • Fully Compatible

      https://docs.mongodb.com/mongoid/master/tutorials/mongoid-documents/#read-only-attributes says:

      You can tell Mongoid that certain attributes are read-only. This will allow documents to be created with these attributes, but changes to them will be ignored when using mass update methods such as update_attributes:

      Individual assignment appears to be equally ignored:

      band = Band.create(name: "Placebo")
      => #<Band _id: 6064c0b62c97a64f8c38172b, name: "Placebo", origin: nil>
      irb(main):074:0> band.name='x'
      => "x"
      irb(main):075:0> band
      => #<Band _id: 6064c0b62c97a64f8c38172b, name: "Placebo", origin: nil>
      irb(main):076:0> band.name
      => "Placebo"
      

      The individual assignment should either raise ReadonlyAttribute instead of being ignored like that or this behavior should be documented.

      Alternatively, if this is the desired behavior, adding a use case to documentation would help clarify when one should use read-only attributes.

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: