ruby on rails 3 - How to set Mongoid fields from within the class -
i lost scope of variables in rails mongoid. (probably due lack of coffee).
all want, way set fields within application, way can find this, calling write_attribute
.
class example include mongoid::document field :foo def bar @foo = "meh" end def hmpf foo = "blah" end def baz write_attribute(:foo, "meh") end end e.bar #=> "meh" e.foo #=> nil e.hmpf #=> "blah" e.foo #=> nil e.baz #=> [nil, "meh"] e.foo #=> "meh"
am using scope wrong? why running foo = "bar"
not set field within, works outside: e.foo = "blah"
works trough magic methods.
try adding self
attribute references when working in model's instance methods:
def hmpf self.foo = "blah" end
should trick.
Comments
Post a Comment