-
Type: Bug
-
Resolution: Fixed
-
Priority: Critical - P2
-
Affects Version/s: None
-
Component/s: Attributes, Persistence
-
None
-
Fully Compatible
When fetching a document with only, attempting to set an excluded attribute persists that attribute as nil, e.g.:
Person.create(name: "bob", age: 42) #=> #<Person _id: ..., name: "bob", age: 42> person = Person.only(:name).last #=> #<Person _id: ..., name: "bob", age: nil> person.set(age: 53) Person.last #=> #<Person _id: ..., name: "bob", age: nil>
I would understand if the in-memory document doesn't get updated because of only(:name) but I would certainly expect a persisted age value of 53. After all, that's what set is supposed to do:
Perform a $set operation on the provided field/value pairs and set the values in the document in memory.
Having set(age: 53) result in an age value of nil seems plain wrong.
BTW, calling set on the criteria object works as expected, e.g.:
criteria = Person.where(name: 'bob').only(:name) #=> #<Mongoid::Criteria ...> criteria.set(age: 53) Person.last #=> #<Person _id: ..., name: "bob", age: 53>
- is related to
-
MONGOID-5072 Document readonly attributes ignoring assignment
- Closed
-
MONGOID-4701 Allow writing fields omitted in #only
- Backlog
- related to
-
MONGOID-5032 Hash attribute access/read_attribute fails on embedded field when parent loaded with only
- Closed
- links to