I've tried to find the answer to this, but my Google-fu must be rusty or the keywords involved make results very noisy.
My understanding is that declaring a field in Mongoid creates accessor methods for that field (i.e. field
and field=
).
What I'm unclear on, as the usage seems to be mixed all over the place, is if and/or when I need to call self.field=
to assign a value, and when that saves.
Example code:
class Product
field state, type: String, default: "inactive"
field sku, type: String
def active?
state == 'active'
end
def inactive?
!active?
end
end
It's clear that I can read a value defined by a Mongoid field without calling self.
But if I want to write a value in a field, do I need to call self
? i.e.
# any difference between these?
def activate
state = 'active'
end
def activate
self.state = 'active'
end
TIA. This seems incredibly basic but for the life of me I can't find a clear answer.