today_ai_weather/app/models/city.rb
songtianlun ebaf7a3f34 feat: add countries and regions management
- Implement Country and Region models
- Establish relationships between City, Country, and Region
- Update ActiveAdmin setup for managing countries and regions
- Add localization support for cities and countries in multiple languages
- Create necessary migrations to support the new schema

This update allows for better categorization of cities under their respective countries and regions, enhancing geographical structure and support for multilingual features.
2025-01-21 18:27:26 +08:00

42 lines
958 B
Ruby

class City < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
belongs_to :country
has_many :weather_arts, dependent: :destroy
validates :name, presence: true
validates :latitude, presence: true
validates :longitude, presence: true
delegate :region, to: :country
def to_s
name
end
def localized_name
I18n.t("cities.#{name.parameterize.underscore}")
end
def full_name
"#{name}, #{country}"
end
def should_generate_new_friendly_id?
name_changed? || super
end
def self.ransackable_associations(auth_object = nil)
[ "weather_arts" ]
end
def self.ransackable_attributes(auth_object = nil)
[ "active", "country", "created_at", "id", "id_value", "last_image_generation", "last_weather_fetch", "latitude", "longitude", "name", "priority", "region", "slug", "timezone", "updated_at" ]
end
def latest_weather_art
weather_arts.order(weather_date: :desc).first
end
end