How to fully reload an ActiveRecord to reset it's memoized values?
The good way is the one you already have: Completely recreating the object.
You cannot "reload" the object's memoized values in any easy "Rails" way, as memoizing attributes isn't a function of Rails or ActiveRecord. Neither knows anything about how you're memoizing methods.
You can do that by redefining reload
. For example if you memoized with ||=
you can do that:
class Foo < ActiveRecord::Base
def bar
@bar ||= rand
end
def reload
@bar = nil
super
end
end
For ways to reset all instance variables see Is there a clean API for resetting instance variables on 'reload' in ActiveRecord?
If you use the memoist gem you can call flush_cache
there.