I’ve created a helper method in ApplicationController that allows access to the most recent posts on all pages.
Is there any way I can cache this to stop my application from hitting the database so often?
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :recent_posts
def recent_posts
@recent_posts = Post.published.recent
end
end
I’ve tried cache_action :recent_posts but, from looking at the logs, the application still appears to hit the database.
It sounds like you are looking for fragment caching.
Try putting it in a partial and doing something like this: