Railsを使っている方も、
Rails2.0とは何か?
Railsの開発者David Heinemeier Hansson
ですが、
この特集ではまず、
Rails2.0の進化と変化
とはいうものの、
ActiveRecord
Railsの中でももっとも大きく重要なフレームワークと目されているActiveRecordでも数多くの変更がなされました。
クエリーキャッシュ(Query Cache)
Rails2.
クエリーキャッシュとは、
「SQLのSELECT文発行の結果を保持し、
ただし、
キャッシュがクリアされるのは
実行時のイメージを、
>> ActiveRecord::Base.cache {
User.find(1) # DBから検索、結果がキャッシュに入る
User.find(1) # キャッシュヒット
}
逆に、
class User < ActiveRecord::Base
class << self
def force_find(*args)
uncached { find(*args) }
end
end
end
リスト2では、
非キャッシュのfindを実行したい場合は以下のようになります。
>>User.force_find(:all)
ActiveRecordその他の進化
その他の変更点としては、
- 数値のバリデーションが便利に
- DBのスキーマ変更
(マイグレーション) が簡単に (Sexy migration) - テスト用データの作成が簡単に
(FoxyFixture)
開発の生産性を向上させるこれらの機能については、
ActionPack(ActionController、ActionView、ActionHelper)
つづいて、
Custom Action Delimiter
まず、
map.resource :posts, :collection=>{:comments=> :get}
上のルート定義が行われていた場合、
GET /posts;comments
GET /posts/comments
この変化の経緯は、
ですので、
RESTful Routing
Rails1.
- :name_
prefixが不要に (自動でprefixがつきます) - namespace機能
(ルーティングにネームスペースが追加可能に) - has_
manyとhas_ one Routing
ここでは、
map.resources :articles,
:has_many => [:comments, :tags],
:has_one => :user
これは、
map.resources :articles do |article|
article.resources :comments
article.resources :tags
article.resource :user
end
ActiveRecordで、
Rails1.
部分レイアウト(Partial Layout)
これは、
<% for link in @links do %>
<%= render :partial => 'link',
:layout => 'link_summary',
:locals => {:link => link} %>
<% end %>
上のコードが、
この:layoutオプションで指定したレイアウトを適用し、
<a href="<%=link.url%>"><%= link.title %></a>
<div id="comment"><%=link.comment%></div>
以下が、
<div id="link_<%= link.id %>">
<%= yield %>
</div>
ActionPackその他の進化
そのほか、
- 画像用、
CSS用といった複数のリソース用サーバがあるようにブラウザに思い込ませる (AssetServers) - 複数のCSSや、
Javascriptファイルを一つにまとめることで並列にダウンロードさせる (AssetCaching) - セッションの保持がRuby PStoreからクッキーに変更
(Cookie-based Session)
Ajaxを縦横に用いた即時のレスポンスが必要なアプリケーションでは重宝する拡張であると思います。
rake
Rails自身の変化につづいて、
Rails2.
routes(Route Listing Task)
Rails1.
このタスクではその複雑さを少しでも軽減するため、
% rake routes|grep DELETE DELETE /hellos/:id {:controller=>"hellos", :action=>"destroy"} DELETE /hellos/:id.:format {:controller=>"hellos", :action=>"destroy"}
db:rollback
MigrationのバージョンをSTEP=Nで指定したNの分だけ過去に戻します。
rake db:rollback STEP=1
Rails2.
Rails2.
notes:todo、notes:fixme
これはRails2.
% rake notes:todo app/controllers/hellos_controller.rb: * [ 5] implement
RailsCoreからなくなったもの
最後に、
フレームワークのスリム化のため、
例を挙げると、
それらとは異なり、
この一点のみで、
Rails2.0インストール
かけあしで、
RubyGemsのインストール
まず、
Rails2.0と必要なパッケージをインストール
RubyGemsのインストールが完了しましたら、
% gem install rails sqlite3 sqlite3-ruby ruby-debug
はじめての Rails2.0
それでは、
Rails2.
% rails hello # Railsアプリケーションフォルダ作成 % cd hello # helloフォルダに移動 % ./script/plugin install scaffolding # scaffolding pluginをインストール % ./script/generate scaffold hello # helloコントローラ、モデル、ビューを作る % rake db:migrate # DBスキーマを作成
さてこれで準備はできました。
Railsアプリケーションをカスタマイズしていきましょう。
# GET /hellos
# GET /hellos.xml
def index
@greeting = "こんにちは" # この行を追加
@hellos = Hello.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @hellos }
end
end
こんどはViewを編集します。Rails2.
index actionのなかに、
<h1>Listing hellos</h1>
<table>
<tr>
<th>Message</th>
<th><%= @greeting %></th> <!-- この行を追加 -->
</tr>
<% for hello in @hellos %>
<tr>
<td><%=h hello.message %></td>
<td><%= link_to 'Show', hello %></td>
<td><%= link_to 'Edit', edit_hello_path(hello) %></td>
<td><%= link_to 'Destroy', hello, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New hello', new_hello_path %>
まとめと次回予告
以上、