How to cache a query in Ruby on Rails 3

后端 未结 2 1752
一生所求
一生所求 2021-02-03 12:37

I have the following query in my application

@categories = Category.joins(:posts).select(\'distinct categories.*\').order(\'label\')

This query

2条回答
  •  囚心锁ツ
    2021-02-03 13:00

    You can use fragment caching for the part of your view template that displays the categories. This means that the categories will be served from the cache store and the query will only be executed once until the cache is expired (by using the expire_fragment method).

    • Fragment Caching Rails Guide

提交回复
热议问题