[サイトマップへ] |
Eclipse を使って,Ruby on Rails の scaffold アプリケーションを扱ってみます.
※ 参考Webページ
【関係するディレクトリとファイル(主要なもの)】
【この Web ページで行うこと】
※ (参考)コマンド操作で行う場合:
cd hoge rails new .
※ (参考)コマンド操作で行う場合:
cd hoge ruby script/rails generate scaffold order_record id:integer year:integer month:integer day:integer customer_name:text product_name:text unit_price:float qty:integer created_at:timestamp updated_at:timestamp
■ SQLite3 でのマイグレーション・ファイルの設定例:
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end end end
■ SQLite3 以外でのマイグレーション・ファイルの設定例:
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end #add constraints execute "ALTER TABLE order_records ADD CONSTRAINT c1_order_records CHECK ( year > 2008 );" execute "ALTER TABLE order_records ADD CONSTRAINT c2_order_records CHECK ( month >= 1 AND month <= 12 );" execute "ALTER TABLE order_records ADD CONSTRAINT c3_order_records CHECK ( day >= 1 AND day <= 31 );" execute "ALTER TABLE order_records ADD CONSTRAINT c4_order_records CHECK ( unit_price > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c5_order_records CHECK ( qty > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c6_order_records CHECK ( ( unit_price * qty ) < 200000 );" end end
※ (参考)コマンド操作で行う場合:
rake db:create rake db:migrate
※ (参考)コマンド操作で行う場合:
ruby scripts/rails server
※ この Web ページの手順をなぞる場合は,sqlite3 パッケージのインストールも済んでいること
mysql, postgresql, sqlite3, oracle などが指定できます.
ここでは Ruby on Rails の学習を行いたいので,SQLite バージョン 3を使うことにします. Ruby から SQLite のデータベースを扱うようなプログラムの作成については, 別の Web ページで詳しく説明しています.
実際に使うには,データベースの権限の設定,複数のアプリケーションプログラムでのデータベースの共有を行うでしょうから, SQLite ではなく,MySQL 等を使うことを検討してください.
ここでは,下記のコマンド操作と同等のことを,Eclipse の GUI を用いて行います.
cd hoge rails new .
新規の Rails プロジェクトを作成したいので 「ファイル (File)」→「 新規 (New)」 →「 プロジェクト (Project)」と操作する.
Rails プロジェクトが現れてないというときは, Aptana の Web ページ http://www.aptana.com/products/radrails で, Eclipse プラグイン版 Aptana Studio の URL の情報を入手.その後, 「ヘルプ (help)」→「 新規ソフトウエアのインストール」 と操作し,
「使用可能なソフトウエア」の中から次のようにをインストールする.
Rails アプリケーション名につけて良いですが,全角文字は避けましょう.分かりやすい名前が良いです. ここでは, Rails アプリケーション名として「hoge」を指定します.
指定したら「Finish」をクリック.
「 Your bundle is complete! ... 」というようなメッセージが出るまでしばらく待つ
既定では,SQLite 3が使われることが分かる. この Web ページの目的である Ruby on Rails の入門用)としては全く問題ない.
データベース名も確認できる.
(Ruby ではなく)JRuby を使う場合にはアダプタの設定を書き換える必要がある.
【SQLite3 を使う場合の設定例】
ここでは,下記のコマンド操作と同等のことを,Eclipse の GUI を用いて行います.
cd hoge ruby script/rails generate scaffold order_record id:integer year:integer month:integer day:integer customer_name:text product_name:text unit_price:float qty:integer created_at:timestamp updated_at:timestamp
この Web ページでは,次のテーブル定義を使います.
■ SQLite3 の場合:
CREATE TABLE order_records (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
year INTEGER NOT NULL,
month INTEGER NOT NULL,
day INTEGER NOT NULL,
customer_name TEXT NOT NULL,
product_name TEXT NOT NULL,
unit_price REAL NOT NULL,
qty INTEGER NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL,
updated_at DATETIME );
CREATE TABLE order_records (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
year INTEGER NOT NULL CHECK ( year > 2008 ),
month INTEGER NOT NULL CHECK ( month >= 1 AND month <= 12 ),
day INTEGER NOT NULL CHECK ( day >= 1 AND day <= 31 ),
customer_name TEXT NOT NULL,
product_name TEXT NOT NULL,
unit_price REAL NOT NULL CHECK ( unit_price > 0 ),
qty INTEGER NOT NULL DEFAULT 1 CHECK ( qty > 0 ),
created_at DATETIME NOT NULL,
updated_at DATETIME,
CHECK ( ( unit_price * qty ) < 200000 ) );
「コマンド (Commands)」→ 「Rails」→ 「Call Generate Script」
「order_record」の部分はモデル名(単数形)です.自由に設定できます.Rails の流儀では,モデル名には単数形の名詞を使います.
下記の通り設定し,OKをクリック.
データ型については,別の Web ページで説明します.
id:integer year:integer month:integer day:integer customer_name:text product_name:text unit_price:float qty:integer created_at:timestamp updated_at:timestamp
【ここで試している設定の要点】
※「Call Generate Script」が選べないと言う場合には,Eclipseo 内のコンソール(Console)で、次のコマンドを実行する.
ruby script/rails generate scaffold order_record id:integer year:integer month:integer day:integer customer_name:text product_name:text unit_price:float qty:integer created_at:timestamp updated_at:timestamp
マイグレーション定義ファイルには,データベースのテーブル定義と一貫性制約の記述等を行います.
【参考 Web ページ】
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html
db/migrate の下にあるマイグレーション・ファイルをクリックする. 下の図での 20110907090702 は実際の日付に読み替えてください.
いま確認したマイグレーション・ファイルを書き換えて,一貫性制約を記述したい.
記述したい一貫性制約は,SQL を使って次のように書くことができるとする.問題は,これを,マイグレーション・ファイルにどう書くか,ということ.
※ SQL の文法と意味については,ここでは説明しないので, 別の Web ページを見てください.
◆ 一貫性制約を含むテーブル定義の例 (SQL での記述)
この Web ページでは,次のテーブル定義を使います.
■ SQLite3 の場合:
CREATE TABLE order_records (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
year INTEGER NOT NULL,
month INTEGER NOT NULL,
day INTEGER NOT NULL,
customer_name TEXT NOT NULL,
product_name TEXT NOT NULL,
unit_price REAL NOT NULL,
qty INTEGER NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL,
updated_at DATETIME );
■ SQLite3以外 の場合:
CREATE TABLE order_records (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
year INTEGER NOT NULL CHECK ( year > 2008 ),
month INTEGER NOT NULL CHECK ( month >= 1 AND month <= 12 ),
day INTEGER NOT NULL CHECK ( day >= 1 AND day <= 31 ),
customer_name TEXT NOT NULL,
product_name TEXT NOT NULL,
unit_price REAL NOT NULL CHECK ( unit_price > 0 ),
qty INTEGER NOT NULL DEFAULT 1 CHECK ( qty > 0 ),
created_at DATETIME NOT NULL,
updated_at DATETIME,
CHECK ( ( unit_price * qty ) < 200000 ) );
◆ ここで行うこと: 上記のテーブル定義と等価なものをマイグレーション・ファイルに記述したいということ
SQL の NOT NULL は非空制約であり, SQL の DEFAULT はデフォルト値 (default value) の指定である.
「ActiveRecord::ConnectionAdapters::TableDefinition#column」の記述によれば,
デフォルト値を SQL の「NULL」にしたい場合には,マイグレーション・ファイルでは 「:default => nil」のように書く
UNIQUE は一意制約であり, CHECK (...) は更新時にチェックされる式 であり, REFERENCES は参照整合性制約である.
■ SQLite3 でのマイグレーション・ファイルの設定例:
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end end end
■ SQLite3 以外でのマイグレーション・ファイルの設定例:
http://guides.rails.info/migrations.html の記述によれば, マイグレーション・ファイル内に execute ... を含めることになります.実例を下に載せています。
SQLite3 には ADD CONSTRAINT の機能が実装されていないため、下のプログラムは動きません(2011/09/01 時点).
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end #add constraints execute "ALTER TABLE order_records ADD CONSTRAINT c1_order_records CHECK ( year > 2008 );" execute "ALTER TABLE order_records ADD CONSTRAINT c2_order_records CHECK ( month >= 1 AND month <= 12 );" execute "ALTER TABLE order_records ADD CONSTRAINT c3_order_records CHECK ( day >= 1 AND day <= 31 );" execute "ALTER TABLE order_records ADD CONSTRAINT c4_order_records CHECK ( unit_price > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c5_order_records CHECK ( qty > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c6_order_records CHECK ( ( unit_price * qty ) < 200000 );" end end
rake db:create rake db:migrate
ここでは,Eclipse 内の「コンソール (Console)」を使い,下記のコマンド操作を行う
rake -T
App Explorer で右クリックし,「Rake」
→
「
ここでは,Eclipse 内の「コンソール (Console)」を使い,下記のコマンド操作を行う
「UTF-8」のように表示される.
App Explorer で右クリックし,「Rake」
→
「
App Explorer で右クリックし,「Rake」
→
「
メッセージから,新しいテーブルが定義されたことが分かります.
App Explorer で右クリックし,「Rake」
→
「Run Server」
と操作する.
WEBrick が起動していることが確認できます.ポート番号が 3000 であることも確認できます.
Web ブラウザで
http://127.0.0.1:3000
を指定します.
Rails サーバと通信して,次のような Web ページが表示されます.
※ Windows のファイヤウオール機能により,ポート番号 3000 との通信が遮断されている場合があるので,
ポート番号 3000 の通信については解除しておくこと.
rake db:charset
rake db:migrate
Rails サーバの起動と動作確認