has_and_belongs_to_many Cuisines の Shop モデルがあります。コントローラーのアクションと編集フォームは次のとおりです。
app/controllers/shops_controller.rb
class ShopsController < ApplicationController
def edit
@shop = Shop.where(id: params[:id]).first
@cuisines = Cuisine.asc(:name)
end
end
app/views/shops/edit.html.haml
= simple_nested_form_for @shop do |f|
.control-group
.control-label
= f.link_to_add 'Cuisine', :cuisines
.controls
= f.fields_for :cuisines do |cuisine|
= cuisine.input :id, label: false, collection: @cuisines
= link_to t_action(:remove)
問題は、@cuisines がその店の料理の数だけ評価されることです。初めてクエリを実行した後、クエリはキャッシュされません。
クエリを減らすためにさまざまなソリューションを検索して試した後、次のいずれかを使用すると役立つことがわかりました。
@cuisines = Cuisine.asc(:name).to_a
また
@cuisines = Cuisine.asc(:name).entries
また
@cuisines = Cuisine.asc(:name).cache
自動キャッシュなどを行うための他のオプションを提案できますか?