I have a problem with making changes to an object from within another model as well as within the object's model. I have the following models:
class Foo < ActiveRecord::Base
has_many :bars
def do_something
self.value -= 1
# Complicated code doing other things to this Foo
bars[0].do_other
save!
end
end
class Bar < ActiveRecord::Base
belongs_to :foo
def do_other
foo.value += 2
foo.save!
end
end
If I have a Foo
object with value
set to 1, and call do_something
on it, I can see from my database logs the following two operations:
Foo Update (0.0s) UPDATE "foos" SET "value" = 2 WHERE "id" = 1
Foo Update (0.0s) UPDATE "foos" SET "value" = 0 WHERE "id" = 1
... so do_something is presumably caching the self
object. Can I avoid this, other than by moving the save!
s around?