Instant Rails ActiveRecord の機能 その1

ActiveRecord はモデルに対応づけされたデータベーステーブルに対して
登録、検索、更新、削除の操作方法を提供してくれる

登録を行うメソッド

オブジェクトを作成し実行するインスタンスメソッドと
クラスに静的に用意されているクラスメソッドがある

メソッド名 引数 戻り値 備考
save なし 登録に成功 true、失敗 false を返す オブジェクトを作成し、そのオブジェクトに対してsaveメソッドを実行
create(value1,value2,…) value:属性名 => 値 新規登録されたモデル 内部でnewを実行後、saveメソッドを実行

検索を行うメソッド

メソッド名 引数 戻り値 備考
find(id) id:主キー 検索対象モデル 主キーを使った場合の検索
find(:first) なし 検索対象モデル テーブルの先頭行を取得
find(:all) なし 検索対象モデルが要素になっている配列 テーブルの全データを取得
find(:conditions) 検索対象モデル
find(:offset) 検索対象モデル
find(:limit) 検索対象モデル
find(:order) 検索対象モデル
find(:select) 検索対象モデル
find(:group) 検索対象モデル
find(:joins) 検索対象モデル
find(:readonly) 検索対象モデル
find(:include) 検索対象モデル
find() 検索対象モデル
find() 検索対象モデル
find() 検索対象モデル

Instant Rails ActiveRecord を利用する その3

テストクラス

テストクラスに名前をつける時は命名規約に従って
テストする対象のクラス名の末尾に Test とつけたクラス名を使う
テストクラスのファイル名は、テストする対象のクラス名のうしろに _test を付記したものになる
user クラスのテストクラスは UserTest、ファイル名は user_test.rb となる

テストメソッドはメソッド名が test_ で始まるのが決まり
Rails にはテストデータを別ファイルに定義し、テスト実行時に削除する仕組みがある
fixtures :users がテストで使用するデータをデータベースにセットするための記述

テストデータと fixtures について

Rails で使用するテストデータは test\fixtures ディレクトリ内に YAML ファイルとして用意
user モデルの動作確認のため、test\fixtures\users.yml と test\unit\user_test.rb を編集

testActiveRecord>ruby test\unit\user_test.rb
Loaded suite test/unit/user_test
Started
..
Finished in 0.109 seconds.
 
2 tests, 4 assertions, 0 failures, 0 errors

Instant Rails ActiveRecord を利用する その2

ActiveRecord とは

Rails では、MVCのM(モデル)を扱うために ActiveRecord というパッケージを利用する
データベースのテーブルやビューを意識することなく簡単にデータアクセスできる
これにより、開発者はビジネスロジックのプログラミングに専念できる

ActiveRecord はデータベース製品の違いを吸収する仕組みを持っている
このため、データベース変更に伴うソースコードの変更は局所に抑える事が出来る

ActiveRecord を使う

RailsActiveRecord を使うということは、モデルを生成し操作するということ
コマンドラインから、スクリプトを利用してモデルを生成する

rails_apps>ruby script/generate model [生成するモデル名]

実際にテーブルを作成し、それに対するモデルを作成する
phpMyAdmin を利用して、データベースを作成する
そして、SQL文を phpMyAdmin で実行し、userテーブルを作成する

create table users (
  id int not null auto_increment,
  name varchar(20) not null,
  profile varchar(80) not null,
  primary key (id));

ここで、config\database.yml を確認

development:
 adapter: mysql
 database: testActiveRecord_development
 username: root
 password:
 host: localhost

OKそうなので、userモデルを作成

rails_apps\testActiveRecord>ruby script\generate model user
   exists app/models/
   exists test/unit/
   exists test/fixtures/
   create app/models/user.rb
   create test/unit/user_test.rb
   create test/fixtures/users.yml
   create db/migrate
   create db/migrate/001_create_users.rb

生成された直後の app\models\user.rb の内容

class User < ActiveRecord::Base
end

テストクラスを使って ActiveRecord クラスを動かす

テスト環境を確認すると \test\unit\user_test.rb がある

「\test\unit\user_test.rb」
require File.dirname(__FILE__) + '/../test_helper'

class UserTest < Test::Unit::TestCase
 fixtures :users

 # Replace this with your real tests.
 def test_truth
  assert true
 end
end

実際に、このテストクラスを実行

testActiveRecord>ruby test\unit\user_test.rb
Loaded suite test/unit/user_test
Started
.
Finished in 0.953 seconds.
 
1 tests, 1 assertions, 0 failures, 0 errors

テスト成功!って、モデル、テストクラスも生成したままの状態なので
これでテストクラスを使うことができる状態にあることが確認できた

Instant Rails ActiveRecord を利用する その1

rails_apps>rails testActiveRecord
   create
   create app/controllers
   create app/helpers
   create app/models
   create app/views/layouts
   create config/environments
   create components
   create db
   create doc
   create lib
   create lib/tasks
   create log
   create public/images
   create public/javascripts
   create public/stylesheets
   create script/performance
   create script/process
   create test/fixtures
   create test/functional
   create test/integration
   create test/mocks/development
   create test/mocks/test
   create test/unit
   create vendor
   create vendor/plugins
   create tmp/sessions
   create tmp/sockets
   create tmp/cache
   create tmp/pids
   create Rakefile
   create README
   create app/controllers/application.rb
   create app/helpers/application_helper.rb
   create test/test_helper.rb
   create config/database.yml
   create config/routes.rb
   create public/.htaccess
   create config/boot.rb
   create config/environment.rb
   create config/environments/production.rb
   create config/environments/development.rb
   create config/environments/test.rb
   create script/about
   create script/breakpointer
   create script/console
   create script/destroy
   create script/generate
   create script/performance/benchmarker
   create script/performance/profiler
   create script/process/reaper
   create script/process/spawner
   create script/process/inspector
   create script/runner
   create script/server
   create script/plugin
   create public/dispatch.rb
   create public/dispatch.cgi
   create public/dispatch.fcgi
   create public/404.html
   create public/500.html
   create public/index.html
   create public/favicon.ico
   create public/robots.txt
   create public/images/rails.png
   create public/javascripts/prototype.js
   create public/javascripts/effects.js
   create public/javascripts/dragdrop.js
   create public/javascripts/controls.js
   create public/javascripts/application.js
   create doc/README_FOR_APP
   create log/server.log
   create log/production.log
   create log/development.log
   create log/test.log

Instant Rails データベースを作る

phpMyAdmin というDB管理ツールを使ってみる

  • データベースを作る
  • プロジェクトを作る

rails_apps>rails -d mysql addressBook
   create
   create app/controllers
   create app/helpers
   create app/models
   create app/views/layouts
   create config/environments
   create components
   create db
   create doc
   create lib
   create lib/tasks
   create log
   create public/images
   create public/javascripts
   create public/stylesheets
   create script/performance
   create script/process
   create test/fixtures
   create test/functional
   create test/integration
   create test/mocks/development
   create test/mocks/test
   create test/unit
   create vendor
   create vendor/plugins
   create tmp/sessions
   create tmp/sockets
   create tmp/cache
   create tmp/pids
   create Rakefile
   create README
   create app/controllers/application.rb
   create app/helpers/application_helper.rb
   create test/test_helper.rb
   create config/database.yml
   create config/routes.rb
   create public/.htaccess
   create config/boot.rb
   create config/environment.rb
   create config/environments/production.rb
   create config/environments/development.rb
   create config/environments/test.rb
   create script/about
   create script/breakpointer
   create script/console
   create script/destroy
   create script/generate
   create script/performance/benchmarker
   create script/performance/profiler
   create script/process/reaper
   create script/process/spawner
   create script/process/inspector
   create script/runner
   create script/server
   create script/plugin
   create public/dispatch.rb
   create public/dispatch.cgi
   create public/dispatch.fcgi
   create public/404.html
   create public/500.html
   create public/index.html
   create public/favicon.ico
   create public/robots.txt
   create public/images/rails.png
   create public/javascripts/prototype.js
   create public/javascripts/effects.js
   create public/javascripts/dragdrop.js
   create public/javascripts/controls.js
   create public/javascripts/application.js
   create doc/README_FOR_APP
   create log/server.log
   create log/production.log
   create log/development.log
   create log/test.log

C:\InstantRails\rails_apps>

  • データベース設定確認

「config\database.yml」
development:
 adapter: mysql
 database: addressBook_development
 username: root
 password:
 host: localhost

  • データベース設定項目
設定項目 項目の意味
adapter 使用するデータベースの種類
database 使用するデータベース名
username データベース接続に使用するユーザー名
password データベース接続に使用するパスワード
host データベースが動いているホスト名
  • アプリケーションの枠組みを作る

addressBook>ruby script/generate scaffold address address
   exists app/controllers/
   exists app/helpers/
   create app/views/address
   exists app/views/layouts/
   exists test/functional/
 dependency model
   exists app/models/
   exists test/unit/
   exists test/fixtures/
   create app/models/address.rb
   create test/unit/address_test.rb
   create test/fixtures/addresses.yml
   create app/views/address/_form.rhtml
   create app/views/address/list.rhtml
   create app/views/address/show.rhtml
   create app/views/address/new.rhtml
   create app/views/address/edit.rhtml
   create app/controllers/address_controller.rb
   create test/functional/address_controller_test.rb
   create app/helpers/address_helper.rb
   create app/views/layouts/address.rhtml
   create public/stylesheets/scaffold.css

  • アプリケーションの動作確認

Instant Rails 基本的な処理を作成

Todo モデルを介在して todos テーブルのデータ作成(Create)、読み出し(Read or Retrieve)

更新(Update)、削除(Delete or Destroy)つまり「CRUD操作」用プログラムを作成

アプリケーションの制御や表示のコードを生成するため scaffold 「足場」というコマンドを利用

todo_app> ruby script/generate scaffold Todo
   exists app/controllers/
   exists app/helpers/
   create app/views/todos
   exists app/views/layouts/
   exists test/functional/
 dependency model
   exists app/models/
   exists test/unit/
   exists test/fixtures/
  identical app/models/todo.rb
  identical test/unit/todo_test.rb
  identical test/fixtures/todos.yml
   create app/views/todos/_form.rhtml
   create app/views/todos/list.rhtml
   create app/views/todos/show.rhtml
   create app/views/todos/new.rhtml
   create app/views/todos/edit.rhtml
   create app/controllers/todos_controller.rb
   create test/functional/todos_controller_test.rb
   create app/helpers/todos_helper.rb
   create app/views/layouts/todos.rhtml
   create public/stylesheets/scaffold.css

ブラウザで動作確認 http://localhost:3000/todos アクセス

とりあえず、Todoを入力