- 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.
22 lines
690 B
Ruby
22 lines
690 B
Ruby
class ArtsController < ApplicationController
|
|
def index
|
|
@regions = Region.all
|
|
@current_region = Region.find(params[:region]) if params[:region].present?
|
|
|
|
@weather_arts = WeatherArt.includes(city: [ :country ]).includes(:image_attachment)
|
|
|
|
if @current_region
|
|
@weather_arts = @weather_arts.joins(city: :country)
|
|
.where(countries: { region_id: @current_region.id })
|
|
end
|
|
|
|
@weather_arts = if params[:sort] == "oldest"
|
|
@weather_arts.order(created_at: :asc)
|
|
else
|
|
@weather_arts.order(created_at: :desc)
|
|
end
|
|
|
|
@weather_arts = @weather_arts.page(params[:page]).per(12)
|
|
end
|
|
end
|