coding addicted
coding addicted

Reputation: 3430

Rails avoiding queries with fragment caching on a basic show action

I'm playing with fragment caching, I have read the guides and watched the railscast.

I'm trying to make some fragment caching on a basic show action:

Controller:

class PostsController < ApplicationController
  before_action :set_post, only: [:show]

  def show
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.friendly.find(params[:id])
      # @post = Post.find(params[:id])
    end
end

View:

<% cache @post do %>

  <h1><%= @post.title %></h1>
  <%= @post.content %>

<% end %>

Problem: whereas the fragment is build and read (see the logs below) the database is still hit. Is this a normal behaviour?

I'm suspecting the before action filter to trigger the query before the cache is read.

I was suspecting the friendly id system but the query happen with the classic find too.

How should I cache this to avoid query?

Logs:

Started GET "/articles/article-3" for 127.0.0.1 at 2014-08-27 10:05:14 -0400
Processing by PostsController#show as HTML
Parameters: {"id"=>"article-3"}
Post Load (1.2ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."slug" = 'article-3'  ORDER BY "posts"."id" ASC LIMIT 1
Cache digest for app/views/posts/show.html.erb: 18a5c19e6efef2fd1ac4711102048e1c
Read fragment views/posts/3-20140730194235000000000/18a5c19e6efef2fd1ac4711102048e1c (0.5ms)
Rendered posts/show.html.erb within layouts/application (4.8ms)

Upvotes: 3

Views: 530

Answers (1)

dax
dax

Reputation: 11007

Unless your application has only one post, I really don't think you would want to cache the first call - fragment caching will cache a given post, but it still needs to know which post is being accessed.

If you did cache this, every time you loaded the post#show page, regardless of what post you asked for, you would see one post - the post that was cached.

Upvotes: 1

Related Questions