dawarich/app/models/country.rb

20 lines
494 B
Ruby
Raw Permalink Normal View History

2025-05-16 12:51:48 -04:00
# frozen_string_literal: true
class Country < ApplicationRecord
2025-06-25 16:23:43 -04:00
has_many :points, dependent: :nullify
2025-05-16 12:51:48 -04:00
validates :name, :iso_a2, :iso_a3, :geom, presence: true
def self.containing_point(lon, lat)
where("ST_Contains(geom, ST_SetSRID(ST_MakePoint(?, ?), 4326))", lon, lat)
.select(:id, :name, :iso_a2, :iso_a3)
.first
end
2025-05-18 05:17:25 -04:00
def self.names_to_iso_a2
2025-07-22 13:17:28 -04:00
Rails.cache.fetch('countries_names_to_iso_a2', expires_in: 1.day) do
pluck(:name, :iso_a2).to_h
end
2025-05-18 05:17:25 -04:00
end
2025-05-16 12:51:48 -04:00
end