diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 0c12e79..dc0ae62 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,8 @@ class HomeController < ApplicationController def index - @popular_shuffle_arts = WeatherArt.by_popularity(10).shuffle.last(6) - @latest_arts = WeatherArt.latest(12) + @popular_arts = WeatherArt.by_popularity(3) + @random_arts = WeatherArt.random(3) + @latest_arts = WeatherArt.latest(6) @featured_arts = WeatherArt.includes(:city).order(created_at: :desc).limit(5) set_meta_tags( title: "AI-Generated Weather Art", diff --git a/app/models/weather_art.rb b/app/models/weather_art.rb index 344fe8b..f624037 100644 --- a/app/models/weather_art.rb +++ b/app/models/weather_art.rb @@ -31,6 +31,19 @@ class WeatherArt < ApplicationRecord end } + scope :random, ->(limit = 3) { + if ActiveRecord::Base.connection.adapter_name.downcase == "postgresql" + # PostgreSQL 优化版本 + order(Arel.sql("RANDOM()")).limit(limit) + elsif ActiveRecord::Base.connection.adapter_name.downcase == "mysql2" + # MySQL 优化版本 + order(Arel.sql("RAND()")).limit(limit) + else + # SQLite 或其他数据库的通用版本 + order(Arel.sql("RANDOM()")).limit(limit) + end + } + def should_generate_new_friendly_id? weather_date_changed? || city_id_changed? || super end diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb index 425138f..3706f00 100644 --- a/app/views/cities/index.html.erb +++ b/app/views/cities/index.html.erb @@ -93,7 +93,7 @@
<% @current_region.countries.order(:name).each do |country| %>+ <%= art.weather_date.strftime("%B %d, %Y") %> +
+- <%= art.weather_date.strftime("%B %d, %Y") %> -
-