today_ai_weather/app/models/region.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

29 lines
662 B
Ruby

class Region < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
has_many :countries, dependent: :restrict_with_error
has_many :cities, through: :countries
validates :name, presence: true
validates :code, presence: true, uniqueness: true
def to_s
name
end
def localized_name
I18n.t("regions.#{code}")
end
# 模型中允许被搜索的关联
def self.ransackable_associations(auth_object = nil)
[ "countries", "cities" ]
end
# 允许被搜索的属性列表
def self.ransackable_attributes(auth_object = nil)
[ "code", "created_at", "id", "id_value", "name", "slug", "updated_at" ]
end
end