From 95afdf1096768abb9624984531449809efae7225 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Fri, 14 Feb 2025 10:01:24 +0800 Subject: [PATCH] refactor: optimize includes for weather arts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed includes for WeatherArt to optimize query - Removed redundant includes of country in WeatherArt - Simplified region fetching by directly ordering These modifications improve the efficiency of the queries by reducing unnecessary joins and utilizing ActiveRecord’s query capabilities more effectively. The code change does not affect the overall functionality but improves maintainability. --- app/controllers/arts_controller.rb | 2 +- app/controllers/cities_controller.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/arts_controller.rb b/app/controllers/arts_controller.rb index 5fb842c..6fa9182 100644 --- a/app/controllers/arts_controller.rb +++ b/app/controllers/arts_controller.rb @@ -3,7 +3,7 @@ class ArtsController < ApplicationController @regions = Region.all @current_region = Region.find(params[:region]) if params[:region].present? - @weather_arts = WeatherArt.includes(city: [ :country, { country: :region } ]) + @weather_arts = WeatherArt.includes(city: [ :country ]).includes(:image_attachment) if @current_region @weather_arts = @weather_arts.joins(city: :country) diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index 217a57d..05a6c71 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -3,7 +3,7 @@ class CitiesController < ApplicationController before_action :require_admin, only: [ :generate_weather_art ] def index - @regions = Region.includes(:countries).order(:name) + @regions = Region.order(:name) @cities = City.includes(:country, country: :region).order(:name) if params[:query].present?