Class: CreateUsZipcodeModels

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/us_zipcode/templates/migration.rb

Class Method Summary collapse

Class Method Details

.downObject



36
37
38
39
40
# File 'lib/generators/us_zipcode/templates/migration.rb', line 36

def self.down
  drop_table :counties
  drop_table :states
  drop_table :zipcodes
end

.upObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/us_zipcode/templates/migration.rb', line 2

def self.up
  # Zipcodes Table
  create_table :zipcodes do |t|
    t.string :code
    t.string :city
    t.integer :state_id, foreign_key: true
    t.integer :county_id, foreign_key: true
    t.decimal :lat, :precision => 15, :scale => 10
    t.decimal :lon, :precision => 15, :scale => 10
    t.timestamps
  end
  add_index :zipcodes, :code
  add_index :zipcodes, :county_id
  add_index :zipcodes, :state_id
  add_index :zipcodes, [:lat, :lon]

  # States Table
  create_table :states do |t|
    t.string :abbr, :limit => 2
    t.string :name
    t.timestamps
  end
  add_index :states, :abbr

  # Counties Table
  create_table :counties do |t|
    t.integer :state_id
    t.string :name
    t.timestamps
  end
  add_index :counties, :name
  add_index :counties, :state_id
end