today_ai_weather/app/models/country.rb
songtianlun ca3691004f feat: Convert country timezones to JSONB format
- Changed `timezones` attribute from text to JSONB in `Country`.
- Updated related model methods to handle JSONB data.
- Added a migration to convert existing timezone data.
- Used safe navigation operators to prevent errors.

This change improves the storage and management of timezone
information by using the JSONB data type.  It includes data
migration to handle existing timezone data.
2025-02-15 16:51:36 +08:00

32 lines
742 B
Ruby

class Country < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
belongs_to :region, optional: true
belongs_to :subregion, optional: true
has_many :cities, dependent: :restrict_with_error
has_many :states
validates :name, presence: true
validates :code, presence: true, uniqueness: true
validates :iso2, uniqueness: true, allow_blank: true
attribute :timezones, :jsonb
def to_s
name
end
def localized_name
I18n.t("countries.#{code}")
end
def self.ransackable_attributes(auth_object = nil)
[ "code", "created_at", "id", "id_value", "name", "region_id", "slug", "updated_at" ]
end
def self.ransackable_associations(auth_object = nil)
[ "cities", "region" ]
end
end