Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model_instance.reload doesn't flush cache #66

Open
stevenspiel opened this issue May 22, 2017 · 1 comment
Open

model_instance.reload doesn't flush cache #66

stevenspiel opened this issue May 22, 2017 · 1 comment

Comments

@stevenspiel
Copy link

stevenspiel commented May 22, 2017

I would expect that calling #reload on an instance of an ActiveRecord model would flush the cache.

If I have a table named foos with one string column name:

class Foo < ActiveRecord::Base
  extend Memoist

  memoize def change_name!
    name = 'bar'
    save
    name
  end
end
describe Foo do
  describe '#change_name!' do
    subject { Foo.create(name: '') }

    expect { subject.change_name! }.to change { subject.reload.name }from('').to('bar')
  end
end

The above test fails unless #flush_cache is called on subject in the change block. Is this the intended behavior?

@matthewrudy
Copy link
Owner

@stevenspiel I agree this is surprising, and have at various times had solutions for this in my local projects.

As I remember there's no official callback for a reload so to make an instance variable be reset for a reload is a bit of a hack.

I wouldn't mind adding an opportunity optional module that could be prepended, or just add something to the docs.

Something like

module Memoist::ActiveRecordReloadable
  def reload
    flush_cache
    super
  end
end

MyModel.prepend(Memoist::ActiveRecordReloadable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants