-
データベースを利用する
Rails の特徴に一つにデータベースとの連結機能があります。ここでは、商品管理を行う depot アプリを作成します。まず、depot アプリを生成します。
C:\InstantRails\work>rails depot
create
create app/controllers
create app/helpers
このアプリでは、depot_development の名前で データベースを参照します。次に、MySql でデータベースを作成します。mysql
-u root コマンドで ユーザ名 root 、パスワードなしでログインします。mysql> のプロンプトに対し、
create database depot_development;
で、データベースを作成します。mysql のコマンドでは最後に ; が必要です。show databases; で作成したデータベースを確認できます。exit
でmysql を終了します。
C:\InstantRails\work>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.27-community
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database depot_development;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cookbook |
| depot_development |
| mysql |
| typo |
| typo_dev |
| typo_test |
+--------------------+
7 rows in set (0.06 sec)
mysql> exit
Bye
C:\InstantRails\work>
以後、テーブル、コラムの作成が mysql> コマンドでも可能ですが、Rails では、Ruby のメソッドで行うことができます。Ruby
から実行することで、履歴が残り再実行ができます。
-
モデルを作成
まず、model の product のスクリプトを生成します。このスクリプト生成機能で、MVC モデルを維持することができます。これで、データベースに
product テーブル(Excelの表に相当)が生成されます。
C:\InstantRails\work\depot>ruby script/generate model product
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/001_create_products.rb
次に、データベースのマイグレーション(「移動」の意味)を行います。work\depot\db\migrate\001_create_products.rb
を次のように編集し、テーブルにコラム(Excelの列に相当)を追加します。
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end
def self.down
drop_table :products
end
end
次に、rake db:migrate でこの内容をデータベースに反映させます(rake は「かき集める」の意味)。
C:\InstantRails\work\depot>rake db:migrate
(in C:/InstantRails/work/depot)
== CreateProducts: migrating =======================
-- create_table(:products)
-> 0.0310s
== CreateProducts: migrated (0.0310s) ==============
-
コントローラの生成
次にMVCモデルのコントローラの admin を生成します。
C:\InstantRails\work\depot>ruby script/generate controller admin
exists app/controllers/
exists app/helpers/
create app/views/admin
exists test/functional/
create app/controllers/admin_controller.rb
create test/functional/admin_controller_test.rb
create app/helpers/admin_helper.rb
生成される、app/controllers/admin_controller.rb に1行追加します。scaffold は「足場」の意味です。
class AdminController < ApplicationController
scaffold :product
end
これで、データベースの表示、編集、をするページが生成されます。
C:\InstantRails\work\depot>ruby script/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
で、Rails を起動し、 ブラウザのURLを http://localhost:3000/admin とします。以下のページが表示され、New
product で、製品を入力するページが表示されます。
項目を入力し、「create」ボタンを押せば、製品が表示されます。この画面から「編集」も可能です。
-
空入力のチェック
必須項目の入力がない場合、データ生成を防ぐには、depot\app\models\product.rb に次の1行を加えます。
class Product < ActiveRecord::Base
validates_presence_of :title, :description
end
この設定後、description を空行で create すると、以下のメッセージがでて、保存されません。
-
データベースの変更
データベースに新しい項目を追加するには、migration にスクリプトを追加します。
C:\InstantRails\work\depot>ruby script/generate migration add_price
exists db/migrate
create db/migrate/002_add_price.rb
ここで、 002_add_price.rb の先頭の 002 はデータベースの履歴番号で、この番号により必要な更新作業を行うことができます。db/migrate/002_add_price.rb を下記のように編集します。
class AddPrice < ActiveRecord::Migration
def self.up
add_column:products, :price, :decimal, :precision => 8, :scale => 2, :default => 0
end
def self.down
remove_column:products, :price
end
end
rake db:migrate でデータベースを更新します。
C:\InstantRails\work\depot>rake db:migrate
(in C:/InstantRails/work/depot)
== AddPrice: migrating ========================================================
-- add_column(:products, :price, :decimal, {:precision=>8, :scale=>2, :default=>
0})
-> 0.0630s
== AddPrice: migrated (0.0630s) ===============================================
ruby script/server/admin でサーバーを起動し、ホームページを更新すると、price の項目追加が確認できます。