テーブルをゴニョゴニョする。

本の内容をすっ飛ばして、気になって仕方がないテーブル操作をいろいろとやってみる。
商品を管理するproductsというテーブルを作ろう。

$script/generate migration create_products

db/migrate/001_create_products.rbというファイルが作成されるので、それを編集してこんな感じに。

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table(:products) do |table|
      table.column(:name, :string)
      table.column(:description, :text)
      table.column(:price, :float)
      table.column(:release_date, :date)
      table.column(:image, :binary)
      table.column(:valid, :boolean)
      table.column(:stock, :int)
    end
  end

  def self.down
    drop_table(:products)
  end
end

でもって、実際にテーブルを作る。

$rake db:migrate

MySQLで確認をするとこんな感じ。

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| name         | varchar(255) | YES  |     | NULL    |                |
| description  | text         | YES  |     | NULL    |                |
| price        | float        | YES  |     | NULL    |                |
| release_date | date         | YES  |     | NULL    |                |
| image        | blob         | YES  |     | NULL    |                |
| valid        | tinyint(1)   | YES  |     | NULL    |                |
| stock        | int(11)      | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

idは自動でRailsによって自動で作成されるPrimaryKey。
型は抽象化されている。RailsRubyMySQLの対応はこんな感じ。

Railsでの表現 Ruby MySQL
:primary_key Fixnum int(11)
:string String varchar(255)
:text String text
:integer Fixnum int(11)
:float Float float
:datetime Time datetime
:timestamp Time datetime
:time Time datetime
:date Date date
:binary String blob
:boolean Object tinyint(1)

:primary_keyはDEFULT NULL、auto_increment、PRIMARY KEYという属性も設定される。つまりPrimaryKeyで自動で増えていく整数。
列を追加してみる。商品のポイント還元を表すpoint列を追加してみようではないか。

$ script/generate migration add_point_to_products
      exists  db/migrate
      create  db/migrate/002_add_point_to_products.rb

となるので、002_add_point_to_products.rbを編集する。

class AddPointToProducts < ActiveRecord::Migration
  def self.up
    add_column(:products, :point, :integer, :null => false, :default => 0)
  end

  def self.down
    remove_column(:products, :point)
  end
end

ちょっと洒落てNOT NULL属性とDEFULT値として0を指定してみた。
MySQLで確認する。

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| point        | int(11)      | NO   |     | 0       |                |
+--------------+--------------+------+-----+---------+----------------+

おっけー。
0ポイントとはケチくさいので、DEFULT値を100にしてみる。うはは、大盤振る舞いじゃ。

$ script/generate migration update_point_to_products
      exists  db/migrate
      create  db/migrate/003_update_point_to_products.rb

003_update_point_to_products.rbを編集。

class UpdatePointToProducts < ActiveRecord::Migration
  def self.up
    change_column(:products, :point, :integer, :default => 100, :null => false)
  end

  def self.down
    change_column(:products, :point, :integer, :default => 0, :null => false)
  end
end

確認。

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| point        | int(11)      | NO   |     | 100     |                | 
+--------------+--------------+------+-----+---------+----------------+

おっけー。
大盤振る舞いしたけど、やっぱり100ポイントは勿体ないのでテーブルを戻す。ワンマン社長でございます。

rake db:migrate VERSION=002

確認。

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| point        | int(11)      | NO   |     | 0       |                | 
+--------------+--------------+------+-----+---------+----------------+

おっけー。
やっぱ面倒だからポイント止めよう。

$ script/generate migration delete_point_to_products
      exists  db/migrate
      create  db/migrate/004_delete_point_to_products.rb

004_delete_point_to_products.rbを編集。

class DeletePointToProducts < ActiveRecord::Migration
  def self.up
    remove_column(:products, :point)
  end

  def self.down
    add_column(:products, :point, :integer, :default => 100, :null => false)
  end
end

確認。

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment | 
| name         | varchar(255) | YES  |     | NULL    |                | 
| description  | text         | YES  |     | NULL    |                | 
| price        | float        | YES  |     | NULL    |                | 
| release_date | date         | YES  |     | NULL    |                | 
| image        | blob         | YES  |     | NULL    |                | 
| valid        | tinyint(1)   | YES  |     | NULL    |                | 
| stock        | int(11)      | YES  |     | NULL    |                | 
+--------------+--------------+------+-----+---------+----------------+

おっけー。
downメソッドがこれで良いのかわからないけど、とりあえず目的は果たしたので、この辺で。


はじめよう Ruby on Rails

はじめよう Ruby on Rails