金子邦彦研究室プログラミングRuby による Web/データベース・プログラミングAptana Studio を使って,Ruby on Rails を扱ってみる

Aptana Studio を使って,Ruby on Rails を扱ってみる

Aptana Studio を使って,Ruby on Rails の scaffold アプリケーションを扱ってみます.

※ 参考Webページ

【関係するディレクトリとファイル(主要なもの)】

この Web ページで行うこと

前準備

前もって決めておく事項

Rails アプリケーションのディレクトリと基本ファイルの生成

ここでは,下記のコマンド操作と同等のことを,Aptana Studio 3 の GUI を用いて行う.

cd hoge
rails new .
  1. プロジェクトの新規作成の開始

    新規の Rails プロジェクトを作成したいので 「ファイル (File)」→「 新規 (New)」 →「 Rails プロジェクト (Rails Project)」と操作する.

    [image]
  2. Rails アプリケーション名の指定

    Rails アプリケーション名につけて良いですが,全角文字は避けましょう.分かりやすい名前が良いです. ここでは, Rails アプリケーション名として「hoge」を指定します.

    指定したら「Finish」をクリック.

    [image]
  3. コンソールで「完了したこと」の確認

    「 Your bundle is complete! ... 」というようなメッセージが出るまでしばらく待つ

    [image]
  4. コンソールを上の方にスクロールすると、「rails new .」が実行されたということが確認できる.

    [image]
  5. いま作成した Rails プロジェクトが,App Explorer ウインドウに表示される

    [image]
  6. (オプション)データベース設定ファイル config/database.yml の確認

    既定(デフォルト)では,SQLite 3 が使われることが確認できる. この Web ページの目的である Ruby on Rails の入門用)としては全く問題ない.

    データベース名も確認できる.

    [image]
  7. JRuby を使う場合のみ)アダプタの設定

    (Ruby ではなく)JRuby を使う場合にはアダプタの設定を書き換える必要がある.

    【SQLite 3 を使う場合の設定例】

    [image]

Rails アプリケーションのひな形 (scaffold アプリケーション) の生成

ここでは,下記のコマンド操作と同等のことを,Aptana Studio 3 の 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

前もって決めておく事項

rails generate scaffold の実行

  1. Generate スクリプトの呼び出し

    コマンド (Commands)」→ 「Rails」→ 「Call Generate Script

  2. 生成プログラムとして,「Scaffold」を選ぶ

    [image]
  3. モデル名(単数形)の設定

    order_record」の部分はモデル名(単数形)です.自由に設定できる.Rails の流儀では,モデル名には単数形の名詞を使う.

    [image]
  4. 属性の設定

    下記の通り設定し,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
    
    

    [image]

    【ここで試している設定の要点】

  5. 完了の確認

    [image]

※「Call Generate Script」が選べないと言う場合には,Aptana Studio 内のコンソール(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

[image]

マイグレーション・ファイルでのテーブル定義

マイグレーション定義ファイルには,データベースのテーブル定義と一貫性制約の記述等を行う.

関連する外部ページ

https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html

  1. Rails アプリケーションのひな形の作成において生成されたマイグレーション・ファイルの確認

    cc/migrate の下にあるマイグレーション・ファイルをクリックする. 下の図での 20110907031451 は実際の日付に読み替えてください.

    [image]
  2. マイグレーション・ファイルでのテーブル定義と一貫性制約の記述

    いま確認したマイグレーション・ファイルを書き換えて,一貫性制約を記述したい.

    記述したい一貫性制約は,SQL を使って次のように書くことができるとする.問題は,これを,マイグレーション・ファイルにどう書くか,ということ.

    SQL の文法と意味については,ここでは説明しないので, 別の Web ページを見てください.

    一貫性制約を含むテーブル定義の例 (SQL での記述)

    このページでは,次のテーブル定義を使う.

    SQLite 3 の場合:

    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 );
    

    SQLite 3以外 の場合:

    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 ) );
    

    ◆ ここで行うこと: 上記のテーブル定義と等価なものをマイグレーション・ファイルに記述したいということ

    SQLite 3 でのマイグレーション・ファイルの設定例:

    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
    

    [image]

    SQLite 3 以外でのマイグレーション・ファイルの設定例:

    http://guides.rails.info/migrations.html の記述によれば, マイグレーション・ファイル内に execute ... を含めることになる.実例を下に載せています.

    SQLite 3 には 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
    

リレーショナルデータベースの作成

ここでは,Aptana Studio の「コンソール (Console)」を使い,下記のコマンド操作を行う

rake db:create
rake db:migrate
  1. (オプション)rake で実行可能なタスク一覧の表示

    rake -T
    

    [image]
  2. (オプション)データベース・スキーマの版(バージョン)の確認

    rake db:version
    

    [image]
  3. (オプション)データベースのキャラクタ・セットの確認

    「UTF-8」のように表示される.

    rake db:charset
    

    [image]
  4. データベース・マイグレーションの実行によるデータベースの生成

    App Explorer を右クリックし,

    rake db:create
    
  5. マイグレーション・ファイルと rake コマンドを用いたマイグレーションの実行

    メッセージから,新しいテーブルが定義されたことが分かります.

    rake db:migrate
    

    [image]

Rails サーバの起動と動作確認

  1. Rails サーバの起動

    ruby scripts/rails server
    

    WEBrick が起動していることが確認できる.ポート番号が 3000 であることも確認できる.

  2. Rails サーバと通信できることのテスト

    Web ブラウザで http://127.0.0.1:3000 を指定します. Rails サーバと通信して,次のような Web ページが表示される.

    [image]

    Windows のファイヤウオール機能により,ポート番号 3000 との通信が遮断されている場合があるので, ポート番号 3000 の通信については解除しておくこと.

  3. Ctrl + 「c」 による Rails サーバの終了