Set custom cache_key of Fragment Caching

40 views
Skip to first unread message

SeongHoon Ryu

unread,
Sep 1, 2018, 1:38:53 PM9/1/18
to Ruby on Rails: Talk

I want to cache with custom cache_key of Fragment Caching because I don't want to change updated_at.


So, I googled and found monkeypatch(https://gist.github.com/tispratik/9276110). But It just a monkey patch.


Is there a way to set custom cache_key?

Thanks


Steve

unread,
Sep 24, 2018, 11:45:01 AM9/24/18
to Ruby on Rails: Talk
In Rails 5, you can turn cache versioning on and override #cache_version in your model if you'd like to return a different column.

https://github.com/rails/rails/blob/b2eb1d1c55a59fee1e6c4cba7030d8ceb524267c/activerecord/lib/active_record/integration.rb

In earlier versions of Rails, you can pass different column names to #cache_key, e.g., model.cache_key(:last_modified).

In either case, you can probably just override #cache_key in the model.
Reply all
Reply to author
Forward
0 new messages