先日のお題を Ruby on Rails で再現した(ソース公開・その 1)

まずは Model をポチポチと。

> rails g model district name:string --timestamps=false
> rails g model city name:string pref_name:string designated_day:date area:float population:integer district_id:integer --timestamps=false

データをぶち込む。

db/seeds.rb

# coding: utf-8

# (略)

District.create(
  :name => '北海道'
)

District.create(
  :name => '東北'
)

District.create(
  :name => '関東'
)

District.create(
  :name => '中部'
)

District.create(
  :name => '近畿'
)

District.create(
  :name => '中国'
)

District.create(
  :name => '四国'
)

District.create(
  :name => '九州'
)

District.create(
  :name => '沖縄'
)

City.create(
  :name => '札幌市',
  :pref_name => '北海道',
  :designated_day => '1972-04-01',
  :area => 1121.12,
  :population => 1921245,
  :district_id => 1
)

City.create(
  :name => '仙台市',
  :pref_name => '宮城県',
  :designated_day => '1989-04-01',
  :area => 785.85,
  :population => 1049493,
  :district_id => 2
)

City.create(
  :name => 'さいたま市',
  :pref_name => '埼玉県',
  :designated_day => '2003-04-01',
  :area => 217.49,
  :population => 1229479,
  :district_id => 3
)

City.create(
  :name => '千葉市',
  :pref_name => '千葉県',
  :designated_day => '1992-04-01',
  :area => 272.08,
  :population => 963120,
  :district_id => 3
)

City.create(
  :name => '横浜市',
  :pref_name => '神奈川県',
  :designated_day => '1956-09-01',
  :area => 437.38,
  :population => 3691693,
  :district_id => 3
)

City.create(
  :name => '川崎市',
  :pref_name => '神奈川県',
  :designated_day => '1972-04-01',
  :area => 142.7,
  :population => 1430773,
  :district_id => 3
)

City.create(
  :name => '相模原市',
  :pref_name => '神奈川県',
  :designated_day => '2010-04-01',
  :area => 328.83,
  :population => 719412,
  :district_id => 3
)

City.create(
  :name => '新潟市',
  :pref_name => '新潟県',
  :designated_day => '2007-04-01',
  :area => 726.1,
  :population => 812458,
  :district_id => 4
)

City.create(
  :name => '静岡市',
  :pref_name => '静岡県',
  :designated_day => '2005-04-01',
  :area => 1411.85,
  :population => 714513,
  :district_id => 4
)

City.create(
  :name => '浜松市',
  :pref_name => '静岡県',
  :designated_day => '2007-04-01',
  :area => 1558.04,
  :population => 798924,
  :district_id => 4
)

City.create(
  :name => '名古屋市',
  :pref_name => '愛知県',
  :designated_day => '1956-09-01',
  :area => 326.43,
  :population => 2266517,
  :district_id => 4
)

City.create(
  :name => '京都市',
  :pref_name => '京都府',
  :designated_day => '1956-09-01',
  :area => 827.9,
  :population => 1473416,
  :district_id => 5
)

City.create(
  :name => '大阪市',
  :pref_name => '大阪府',
  :designated_day => '1956-09-01',
  :area => 223.0,
  :population => 2670579,
  :district_id => 5
)

City.create(
  :name => '堺市',
  :pref_name => '大阪府',
  :designated_day => '2006-04-01',
  :area => 149.99,
  :population => 842685,
  :district_id => 5
)

City.create(
  :name => '神戸市',
  :pref_name => '兵庫県',
  :designated_day => '1956-09-01',
  :area => 552.26,
  :population => 1544496,
  :district_id => 5
)

City.create(
  :name => '岡山市',
  :pref_name => '岡山県',
  :designated_day => '2009-04-01',
  :area => 789.92,
  :population => 710913,
  :district_id => 6
)

City.create(
  :name => '広島市',
  :pref_name => '広島県',
  :designated_day => '1980-04-01',
  :area => 905.41,
  :population => 1177711,
  :district_id => 6
)

City.create(
  :name => '北九州市',
  :pref_name => '福岡県',
  :designated_day => '1963-04-01',
  :area => 488.78,
  :population => 974287,
  :district_id => 8
)

City.create(
  :name => '福岡市',
  :pref_name => '福岡県',
  :designated_day => '1972-04-01',
  :area => 341.7,
  :population => 1479433,
  :district_id => 8
)

City.create(
  :name => '熊本市',
  :pref_name => '熊本県',
  :designated_day => '2012-04-01',
  :area => 389.54,
  :population => 736010,
  :district_id => 8
)
> rake db:migrate
> rake db:seed

Model 同士のアソシエーションを定義。

app/models/city.rb

class City < ActiveRecord::Base
  belongs_to :district
end

ついでに View と Controller の素も作っておく。

> rails g controller major_cities index show

ルーティングも設定しとく。

config/routes.rb

MajorCity::Application.routes.draw do
  resources :major_cities
  # (略)
end

残りはまた後日。