Rails2.0でブックマークアプリ

Rails2.0はインストール済み、「rails bookmark」でbookmarkアプリの雛形までは作成されているという前提で話をすすめていく。
接続先のデータベース(MySQL)にもテーブルは一つも存在しない。この状態で、itemに対応するModelを生成する。
C:\Projects\OtherProjects\Rails\bookmark>jruby script\generate scaffold item url
:string title:string
create app/models/
create app/controllers/
create app/helpers/
create app/views/items
create app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/items/index.html.erb
create app/views/items/show.html.erb
create app/views/items/new.html.erb
create app/views/items/edit.html.erb
create app/views/layouts/items.html.erb
identical public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/item.rb
identical test/unit/item_test.rb
skip test/fixtures/items.yml
create db/migrate
create db/migrate/001_create_items.rb
create app/controllers/items_controller.rb
identical test/functional/items_controller_test.rb
create app/helpers/items_helper.rb
route map.resources :items
ModelとControllerは無事に生成されている。migrateファイルも生成されているようだ。

テーブルの作成
次に、Migrateを使ってDBにテーブルを追加する。
C:\Projects\OtherProjects\Rails\bookmark\db\migrate>rake db:migrate
(in C:/Projects/OtherProjects/Rails/bookmark)
== 1 CreateItems: migrating ===================================================
-- create_table(:items)
-> 0.0460s
== 1 CreateItems: migrated (0.0460s) ==========================================
DBを確認してみると…
sqlite> .tables
items schema_info
sqlite> .schema items
CREATE TABLE items ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varch
ar(255) DEFAULT NULL, "title" varchar(255) DEFAULT NULL, "created_at" datetime D
EFAULT NULL, "updated_at" datetime DEFAULT NULL);
という感じで、無事テーブルが出来た。サーバーを起動して、アプリケーションが動作しているかどうかを確認する。
C:\Projects\OtherProjects\Rails\bookmark>jruby script\server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-12-22 21:35:02] INFO WEBrick 1.3.1
[2007-12-22 21:35:02] INFO ruby 1.8.5 (2006-12-25) [i386-mswin32]
[2007-12-22 21:35:02] INFO WEBrick::HTTPServer#start: pid=172 port=3000
無事に動作していることを確認。ここで、Itemにdescriptionを追加してみる。DBに列を追加するためには、script\generate migration を使う。
C:\Projects\OtherProjects\Rails\bookmark>ruby script\generate migration AddDescr
iptionToItem description:text
exists db/migrate
create db/migrate/002_add_description_to_item.rb
これで、Migrationファイルは作成完了。ちなみに、やってみるまではわからなかったが、MigrationファイルはMigrationファイル名ごとに1つではなく、アプリケーション全体で1つのバージョンを持つ。ここで生成されたのはバージョン002でMigration名がadd_description_to_itemだが、バージョン001のMigration名はcreate_itemsになっている。ちなみに、現段階ではMigrationファイルが作成されただけで、DBスキーマにはまだ変更は反映されていない。DBスキーマに変更を反映するためには、rake db:migrationを使う。
C:\Projects\OtherProjects\Rails\bookmark>rake db:migrate
(in C:/Projects/OtherProjects/Rails/bookmark)
== 2 AddDescriptionToItem: migrating ==========================================
-- add_column(:items, :description, :text)
-> 0.1100s
== 2 AddDescriptionToItem: migrated (0.1100s) =================================
SQLiteを見てみると…
sqlite> .schema items
CREATE TABLE items ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varch
ar(255) DEFAULT NULL, "title" varchar(255) DEFAULT NULL, "created_at" datetime D
EFAULT NULL, "updated_at" datetime DEFAULT NULL, "description" text);
このように、末尾にdescriptionが追加されていることがわかる。

画面への反映
以上でDBへの列定義追加は完了したが、画面(html.erb)にはDescription表示用の項目が反映されていないため、アプリケーションを起動してもDescriptionは出てこない。ViewにDescription用のコードを追加するには、手動でコードを追加するか、generate scaffoldで生成しなおすかくらいだろうか?
現段階ではscaffoldで生成しなおす方が手っ取り早いので、生成しなおしてみる。ちなみに、普通に生成すると、同じ名前のMigrationファイルが既に存在するために失敗する。
C:\Projects\OtherProjects\Rails\bookmark>ruby script/generate scaffold item url:
string title:string description:text
exists app/models/
exists app/controllers/
exists app/helpers/
exists app/views/items
exists app/views/layouts/
exists test/functional/
exists test/unit/
identical app/views/items/index.html.erb
identical app/views/items/show.html.erb
identical app/views/items/new.html.erb
identical app/views/items/edit.html.erb
identical app/views/layouts/items.html.erb
identical public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/item.rb
identical test/unit/item_test.rb
skip test/fixtures/items.yml
exists db/migrate
Another migration is already named create_items: db/migrate/001_create_items.rb
これを回避するためには、--skip-migrationオプションをつけて実行する。まぁ、失敗しててもViewは出力されているから別にいい気がするけど。
C:\Projects\OtherProjects\Rails\bookmark>ruby script/generate scaffold item url:
string title:string description:text --skip-migration
exists app/models/
exists app/controllers/
exists app/helpers/
exists app/views/items
exists app/views/layouts/
exists test/functional/
exists test/unit/
identical app/views/items/index.html.erb
identical app/views/items/show.html.erb
identical app/views/items/new.html.erb
identical app/views/items/edit.html.erb
identical app/views/layouts/items.html.erb
identical public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/item.rb
identical test/unit/item_test.rb
skip test/fixtures/items.yml
identical app/controllers/items_controller.rb
identical test/functional/items_controller_test.rb
identical app/helpers/items_helper.rb
route map.resources :items
script\serverでサーバーを起動してみると、無事にDescriptionが追加されていることが確認できる。

というわけで、ブックマークアプリはひとまず完成。この後は、1.2で途中まで進めていた内部構造の解析をすすめていく。

関連リンク:
Rails2.0リリース
Rails2.0のscaffoldは前とだいぶ違うらしい
Migration
Rails2.0の変更点
Action Pack: Resources
Action Pack: Multiview
Action Pack: Record identification

コメント