4 changed files with 31 additions and 1 deletions
@ -1,4 +1,7 @@
|
||||
class LocalAuthority < ApplicationRecord |
||||
has_many :local_authority_links, dependent: :destroy |
||||
has_many :linked_local_authorities, class_name: "LocalAuthority", through: :local_authority_links |
||||
|
||||
scope :active, ->(date) { where("start_date <= ? AND (end_date IS NULL OR end_date >= ?)", date, date) } |
||||
scope :england, -> { where("code LIKE ?", "E%") } |
||||
end |
||||
|
||||
@ -0,0 +1,4 @@
|
||||
class LocalAuthorityLink < ApplicationRecord |
||||
belongs_to :local_authority, class_name: "LocalAuthority" |
||||
belongs_to :linked_local_authority, class_name: "LocalAuthority" |
||||
end |
||||
@ -0,0 +1,12 @@
|
||||
class AddLocalAuthorityLink < ActiveRecord::Migration[7.0] |
||||
def change |
||||
create_table :local_authority_links do |t| |
||||
t.references :local_authority |
||||
t.references :linked_local_authority |
||||
|
||||
t.timestamps |
||||
end |
||||
add_foreign_key :local_authority_links, :local_authorities, column: :local_authority_id |
||||
add_foreign_key :local_authority_links, :local_authorities, column: :linked_local_authority_id |
||||
end |
||||
end |
||||
Loading…
Reference in new issue