テスト用のRailsの環境を作成

> gem install sqlite3-ruby ・・・sqlite3がなかったのでインストール
> rails jaspertest
> cd jaspertest
> ruby script/generate model CustomerType
> ruby script/generate model Customer

SQLite3をインストール

SQLite Download Pageから以下の2つのファイルをダウンロード
* sqlite-3_5_8.zip
* sqlitedll-3_5_8.zip
これらのファイルをダウンロードし、解凍したらPATHの通っているところにコピーする。
その後以下のコマンドを実行してRubyからsqlite3を使えるようにする。

> gem install sqlite3-ruby

テーブルを作成

db/migrate/001_create_customer_types.rbを以下のように編集

class CreateCustomerTypes < ActiveRecord::Migration
  def self.up
    create_table :customer_types do |t|
      t.integer :id
      t.string :code, :limit => 3
      t.string :name, :limit => 10
      t.timestamps
    end
  end

  def self.down
    drop_table :customer_types
  end
end

db/migrate/002_create_customers.rbを以下のように編集

class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.integer :id
      t.string :name, :null => false
      t.string :address
      t.integer :customer_type_id
      t.timestamps
    end
  end

  def self.down
    drop_table :customers
  end
end

migrateを実行してテーブルを作成

> rake db:migrate

テスト用のデータをフィクスチャのファイルに入力

test/fixtures/customer_types.yml

type1:
   id: 1
   code: '001'
   name: '支払先'
type2:
   id: 2
   code: '002'
   name: '請求先'
type3:
   id: 3
   code: '003'
   name: '支払・請求'

test/fixtures/customers.yml

custmer_1:
   id: 1
   name: '株式会社 ABC'
   address: '東京都渋谷区'
   customer_type_id: 1
   
custmer_2:
   id: 2
   name: '株式会社 CDE'
   address: '東京都渋谷区'
   customer_type_id: 1
   
custmer_3:
   id: 3
   name: '株式会社 EFG'
   address: '東京都渋谷区'
   customer_type_id: 3
   
custmer_4:
   id: 4
   name: 'XYZ 株式会社'
   address: '東京都渋谷区'
   customer_type_id: 2

テーブルにフィクスチャのデータをロードする

> rake db:fixtures:load

CustomerとCustomerTypeの関連を設定

app/models/customer.rbを以下のようにする

class Customer < ActiveRecord::Base
   belongs_to :customer_type
end