<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hmalipa</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Hmalipa"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Hmalipa"/>
	<updated>2026-06-24T13:08:47Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51475</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51475"/>
		<updated>2011-09-30T04:44:27Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework is refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier to configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax and it’s too big to discuss all&lt;br /&gt;
those in a single article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating these concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51474</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51474"/>
		<updated>2011-09-30T04:43:01Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework is refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier to configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this single article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51463</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51463"/>
		<updated>2011-09-30T04:23:09Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework is refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this single article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51462</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51462"/>
		<updated>2011-09-30T04:21:26Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51461</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51461"/>
		<updated>2011-09-30T04:21:10Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51460</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51460"/>
		<updated>2011-09-30T04:19:34Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Model Abstraction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled Action Pack from Active Record directly,&lt;br /&gt;
and Active Record becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51458</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51458"/>
		<updated>2011-09-30T04:18:42Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51456</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51456"/>
		<updated>2011-09-30T04:17:59Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* AbstractController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Abstract Controller====&lt;br /&gt;
Rails 3 has refactored Action Controller and Action Mailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51455</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51455"/>
		<updated>2011-09-30T04:17:23Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* ActionDispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====Action Dispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into Action Dispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect [http://en.wikipedia.org/wiki/Domain-specific_language DSL] still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51454</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51454"/>
		<updated>2011-09-30T04:15:23Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Development */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
This section presents changes related to code generation commands like scaffold and migration commands. Many of the commands in Rails 2 have aliases in Rails 3 which are easy to remember and hence helps in reducing development phase cycle.&lt;br /&gt;
&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51453</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51453"/>
		<updated>2011-09-30T04:11:01Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* AbstractController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about [http://en.wikipedia.org/wiki/HTTP HTTP] or mail delivery.&lt;br /&gt;
[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] now has Abstract Controller, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This Abstract Controller handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51449</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51449"/>
		<updated>2011-09-30T04:06:53Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Model Abstraction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between Active Record, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ Action Pack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell Action Pack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the Active Model API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51441</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51441"/>
		<updated>2011-09-30T04:00:01Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Modularization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Active Record] and [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller],&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51439</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51439"/>
		<updated>2011-09-30T03:57:55Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Action Controller */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, Action Controller had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51436</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51436"/>
		<updated>2011-09-30T03:57:07Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Action_Controller Action Controller] and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51432</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51432"/>
		<updated>2011-09-30T03:55:40Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Structural */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], Action Controller and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51429</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51429"/>
		<updated>2011-09-30T03:53:20Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch2_2f_vh#Active_Record Action Record], Action View, Action Controller and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51426</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51426"/>
		<updated>2011-09-30T03:51:16Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Structural */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
The structural changes relate to changes architecture. Many of the previously existing components have been realigned to make it better [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller MVC] framework. The following sections talk about changes in all the three components namely, Action Record, Action View, Action Controller and also other supporting components.&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51418</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51418"/>
		<updated>2011-09-30T03:42:47Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations the graph shows basic statements execution time vs Rails version]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations the graph shows complex statements execution time vs Rails version]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51417</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51417"/>
		<updated>2011-09-30T03:40:50Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* AbstractController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[http://en.wikipedia.org/wiki/HTTP HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51405</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51405"/>
		<updated>2011-09-30T03:30:50Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* AbstractController */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
[HTTP http://en.wikipedia.org/wiki/HTTP]. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51400</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51400"/>
		<updated>2011-09-30T03:29:26Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Model Abstraction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and [http://ap.rubyonrails.org/ ActionPack], its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51398</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51398"/>
		<updated>2011-09-30T03:26:35Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a [http://en.wikipedia.org/wiki/Relational_algebra Relational Algebra] for Ruby. It simplifies the&lt;br /&gt;
generation complex of [http://en.wikipedia.org/wiki/SQL SQL] queries and adapts to various [http://en.wikipedia.org/wiki/RDBMS RDBMS] systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51397</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51397"/>
		<updated>2011-09-30T03:23:33Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Modularization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like [http://en.wikipedia.org/wiki/Datamapper DataMapper], to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires.&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51396</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51396"/>
		<updated>2011-09-30T03:22:17Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Modularization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework PrototypeJS] as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51395</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51395"/>
		<updated>2011-09-30T03:21:22Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its [http://en.wikipedia.org/wiki/Object-relational_mapping ORM] layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51394</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=51394"/>
		<updated>2011-09-30T03:19:59Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM[http://en.wikipedia.org/wiki/Object-relational_mapping] layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50487</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50487"/>
		<updated>2011-09-23T03:08:20Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50486</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50486"/>
		<updated>2011-09-23T03:06:55Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Modularization====&lt;br /&gt;
&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Active Record====&lt;br /&gt;
&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50482</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50482"/>
		<updated>2011-09-23T03:05:12Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50480</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50480"/>
		<updated>2011-09-23T03:04:48Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50479</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50479"/>
		<updated>2011-09-23T03:04:36Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
'''Change in query interface'''&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50478</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50478"/>
		<updated>2011-09-23T03:03:17Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself (DRY)''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50477</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50477"/>
		<updated>2011-09-23T03:02:52Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles ''Convention over configuration'' and ''Don't repeat yourself DRY''&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50472</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50472"/>
		<updated>2011-09-23T02:58:13Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* ActionDispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50467</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50467"/>
		<updated>2011-09-23T02:56:02Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* ActionDispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50461</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50461"/>
		<updated>2011-09-23T02:53:25Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 24px&amp;quot;&amp;gt;'''Rails 2 and Rails 3'''&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has undergone many changes from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50459</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50459"/>
		<updated>2011-09-23T02:51:24Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Rails 2 and Rails 3'''&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50457</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50457"/>
		<updated>2011-09-23T02:50:48Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Rails 2 and Rails 3&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50455</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50455"/>
		<updated>2011-09-23T02:50:16Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Rails 2 and Rails 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Rails 2 and Rails 3&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50453</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50453"/>
		<updated>2011-09-23T02:50:00Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Rails 2 and Rails 3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Rails 2 and Rails 3==&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50451</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50451"/>
		<updated>2011-09-23T02:46:49Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* ActionDispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50450</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50450"/>
		<updated>2011-09-23T02:46:33Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* ActionDispatch */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50447</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50447"/>
		<updated>2011-09-23T02:43:32Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|400px|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|400px|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50445</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50445"/>
		<updated>2011-09-23T02:42:33Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
{|&lt;br /&gt;
|[[File:Blog 19small-1-.jpg|alt=Alt text|center|thumb|For basic operations]]&lt;br /&gt;
|[[File:Blog 19big-1-.jpg|alt=Alt text|center|thumb|For complex operations]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50443</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50443"/>
		<updated>2011-09-23T02:41:11Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
[[File:Blog 19small-1-.jpg|alt=Alt text|center|thumb|For basic operations]]&lt;br /&gt;
[[File:Blog 19big-1-.jpg|alt=Alt text|center|thumb|For complex operations]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50442</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50442"/>
		<updated>2011-09-23T02:40:22Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
[[File:Blog 19small-1-.jpg|alt=Alt text|thumb|For basic operations]]&lt;br /&gt;
[[File:Blog 19big-1-.jpg|alt=Alt text|thumb|For complex operations]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50439</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50439"/>
		<updated>2011-09-23T02:38:13Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: /* Performance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
[[File:Blog 19small-1-.jpg|alt=Alt text|For basic operations]]&lt;br /&gt;
[[File:Blog 19big-1-.jpg|alt=Alt text|For complex operations]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50438</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50438"/>
		<updated>2011-09-23T02:36:07Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
[[File:Blog 19big-1.jpg|alt=Alt text|For basic operations]]&lt;br /&gt;
[[File:blog_19big.jpeg|alt=Alt text|For complex operations]]&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50437</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f vh</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_vh&amp;diff=50437"/>
		<updated>2011-09-23T02:35:33Z</updated>

		<summary type="html">&lt;p&gt;Hmalipa: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Rails 2 and Rails 3=&lt;br /&gt;
===Introduction===&lt;br /&gt;
Rails 3 has incorporated significant changes over its 2.x version improving on performance, modularity&lt;br /&gt;
and elegant constructs and thereby reinforcing its principles Convention over configuration and DRY&lt;br /&gt;
again. The internal architecture of the framework was refactored to make it more modular, flexible&lt;br /&gt;
and coherent. Routing component has been revamped and it makes lot easier configure routes.&lt;br /&gt;
Dependency management is also one of the significant improvements observed in 3.0. It is taken care&lt;br /&gt;
by new Bundler component, which automates the process of installing, updating library management.&lt;br /&gt;
The database operations are made much more efficient and closer to relational databases.&lt;br /&gt;
There are many changes in Rails 3 from architectural to syntax changes and it’s too big to discuss all&lt;br /&gt;
those in this article. However, this wiki article presents only major changes that are relevant to class&lt;br /&gt;
and at same time illustrating those concepts with examples discussed in class, especially Cookbook&lt;br /&gt;
Application in Rails 2. It is assumed that readers are familiar with Rails 2. With this, it serves as a&lt;br /&gt;
quick tutorial to understand Rails 3 features.&lt;br /&gt;
===Structural===&lt;br /&gt;
====Ruby Version====&lt;br /&gt;
Rails 3.0 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been&lt;br /&gt;
dropped officially and you should upgrade as early as possible. Rails 3.0 is also compatible with Ruby&lt;br /&gt;
1.9.2.&lt;br /&gt;
====Modularization====&lt;br /&gt;
Versions prior to Rails 3 were strongly coupled to a limited choices of frameworks that formed the&lt;br /&gt;
Rails stack. For example, a standard Rails stack used Active Record as its ORM layer, Action View for&lt;br /&gt;
rendering its views, PrototypeJS as its javascript library and so on.&lt;br /&gt;
However, with Rails 3, it has become more modular, starting with a rails-core, and including the ability&lt;br /&gt;
to opt in or out of specific frameworks. Its default components, like ActiveRecord and ActionController,&lt;br /&gt;
behave like regular plugins. This allows other plugins, like DataMapper, to use exactly the same APIs&lt;br /&gt;
used by ActiveRecord and hence replace it if the end user desires..&lt;br /&gt;
====Active Record====&lt;br /&gt;
Change in query interface:&lt;br /&gt;
In Rails 2, ActiveRecord provides the following finder methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array_of_ids, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:first, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(:all, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;first(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;all(options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates, conditions, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
And the following calculation methods :&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;count(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;average(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;minimum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;maximum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;sum(column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;calculate(operation, column, options)&amp;lt;/li&amp;gt;&lt;br /&gt;
Each of these methods take an options hash&lt;br /&gt;
containing :conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from&lt;br /&gt;
&amp;amp; :lock.&lt;br /&gt;
Starting with Rails 3, supplying any option has been deprecated (an exception here is that count() will&lt;br /&gt;
still accept a :distinct option).&lt;br /&gt;
Example of deprecated usage:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
User.find(:all, :limit =&amp;gt; 1)&lt;br /&gt;
User.find(:all)&lt;br /&gt;
User.find(:first)&lt;br /&gt;
User.first(:conditions =&amp;gt; {:name =&amp;gt; 'vaibhav'})&lt;br /&gt;
User.all(:joins =&amp;gt; :items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 3, Active Record, uses a framework – Arel, which has been taken on as the underpinnings&lt;br /&gt;
of Active Record and is now required for Rails. Arel is a Relational Algebra for Ruby. It simplifies the&lt;br /&gt;
generation complex of SQL queries and adapts to various RDBMS systems. Active Record, through&lt;br /&gt;
the use of Arel, now returns relations on its core methods.&lt;br /&gt;
ActiveRecord in Rails 3 will have the following new finder methods.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;where (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;having (:conditions)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;select&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;group&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;order&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;limit&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;offset&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;joins&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;includes (:include)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;lock&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;readonly&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;from&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All of the above methods returns a Relation. All these methods are defined on the Relation object as&lt;br /&gt;
well, making it possible to chain them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vaibhav = User.where(:name =&amp;gt; 'vaibhav')&lt;br /&gt;
new_users = User.order('users.id DESC').limit(20).includes(:items)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The relation returned also supports the following methods:&lt;br /&gt;
You could call any of the following methods on a relation :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;new(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;create!(attributes)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;find(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;destroy_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete(id_or_array)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;delete_all&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update(ids, updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;update_all(updates)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;exists?&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, in cookbook application&lt;br /&gt;
In rails 2 for fetching all categories which have pizza we execute statement&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.find(:all, :conditions =&amp;gt; {:recipe =&amp;gt;’pizza’})&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Whereas in rails 3 we have a very simple query that achieves same result by&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Category.where(:recipe =&amp;gt; ‘pizza’)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Active Model Abstraction====&lt;br /&gt;
Active Model was extracted while decoupling Rails’ historic connection between ActiveRecord, its&lt;br /&gt;
default ORM, and ActionPack, its controller and view layer. All new ORM plugins (e.g. DataMapper or&lt;br /&gt;
Sequel) now just need to implement Active Model interfaces to work seamlessly with Action Pack. The&lt;br /&gt;
Active Model API itself is fairly small, with a few methods around validation, the ability to determine&lt;br /&gt;
whether an object has persisted or not (which can be safely stubbed by objects without persistence),&lt;br /&gt;
and a number of methods that tell ActionPack how to convert the object into a canonical URL or&lt;br /&gt;
template name. By doing this, Rails has completely decoupled ActionPack from ActiveRecord directly,&lt;br /&gt;
and ActiveRecord becomes just one of many ORMs to implement the ActiveModel API. An example&lt;br /&gt;
is the use of validations (traditionally an Active Record feature) in any Ruby object that adheres to the&lt;br /&gt;
Active Model API.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person &amp;lt; ActiveRecord::Base&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To do the same thing for a plain old Ruby object, simply do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
validates_presence_of :first_name, :last_name&lt;br /&gt;
attr_accessor :first_name, :last_name&lt;br /&gt;
def initialize(first_name, last_name)&lt;br /&gt;
@first_name, @last_name = first_name, last_name&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, In Cookbook application discussed in class we have the recipe model class with&lt;br /&gt;
validations (Rails 2.x code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe &amp;lt; ActiveRecord::Base&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates :title, :presence =&amp;gt; true&lt;br /&gt;
validates :instructions, :presence =&amp;gt; true&lt;br /&gt;
validates :category, :presence =&amp;gt; true&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Rails 3 we achive the same functionality by less code and has validates_presence_of method&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Recipe&lt;br /&gt;
include ActiveModel::Validations&lt;br /&gt;
belongs_to :category&lt;br /&gt;
validates_presence_of :title, :instructions, :category&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other modules available in Active Model include:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;AttributeMethods: Makes it easy to add attributes that are set like table_name :foo&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Callbacks: ActiveRecord-style lifecycle callbacks.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Naming: Default implementations of model.model_name, which are used by ActionPack (for&lt;br /&gt;
instance, when you do render :partial =&amp;gt; model&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Observing: ActiveRecord-style observers&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
====Action Controller====&lt;br /&gt;
Previously, ActionController had a number of disparate elements all in one place. This monolithic&lt;br /&gt;
component is now divided into multiple pieces. Two significant componets are:&lt;br /&gt;
=====ActionDispatch=====&lt;br /&gt;
The dispatcher functionality has been moved into ActionDispatch, with the code inside tightened&lt;br /&gt;
up and really made a conceptual component. This has resulted in a new routing API. While the old&lt;br /&gt;
map.connect DSL still works just fine, the new standard DSL is less verbose and more readable.&lt;br /&gt;
'''old way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
map.connect &amp;quot;/main/:id&amp;quot;, :controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''new way'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
match &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
First, the routes are attached to the application, which is now its own object. Second, map is no longer&lt;br /&gt;
required, and the new DSL (match/to) is more expressive. Finally, we have a shortcut for controller/&lt;br /&gt;
action pairs&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(&amp;quot;main#home&amp;quot; is {:controller =&amp;gt; &amp;quot;main&amp;quot;, :action =&amp;gt; &amp;quot;home&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Another useful shortcut allows you to specify the method more simply than before:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Piazza::Application.routes do&lt;br /&gt;
post &amp;quot;/main/:id&amp;quot;, :to =&amp;gt; &amp;quot;main#home&amp;quot;, :as =&amp;gt; :homepage&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The :as in the above example specifies a named route, and creates the homepage_url et al helpers as&lt;br /&gt;
in Rails 2.&lt;br /&gt;
&lt;br /&gt;
'''Simple routes'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.resources :posts,:member={:confirm=&amp;gt;:post,:notify=&amp;gt;:post} do |post|&lt;br /&gt;
post.resources :comments,:member=&amp;gt;{:preview=&amp;gt;:post},:collection=&amp;gt;{:archived=&amp;gt;:get}&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;\pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Resources :posts do&lt;br /&gt;
member do&lt;br /&gt;
post :confirm&lt;br /&gt;
get :notify&lt;br /&gt;
end&lt;br /&gt;
Resources :comments do&lt;br /&gt;
post :preview, &amp;lt;img src=&amp;quot;http://vaibhav.com/images/smilies/icon_surprised.gif&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:member&lt;br /&gt;
get :archived, &amp;lt;img src=&amp;quot;http://harsha.com/images/smilies/icon_surprised.gif&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
n=&amp;gt;:collection&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Root mapping'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
map.root :controller=&amp;gt;”user”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
root :to=&amp;gt;”user#index”&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Legacy Route'''&lt;br /&gt;
Rails 2&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Map.connect ':controller/:action/:id'&lt;br /&gt;
Map.connect ':controller/:action/:id.:format'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Rails 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Match ':controller(/:action(/:id(/:format)))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
====AbstractController====&lt;br /&gt;
Rails 3 has refactored ActionController and ActionMailer to inherit from a common superclass:&lt;br /&gt;
AbstractController. AbstractController includes the basic concept of controllers, rendering, layouts,&lt;br /&gt;
helpers, and callbacks, but nothing about HTTP or mail delivery.&lt;br /&gt;
ActionController now has AbstractController, a base superclass that is separated from the notions of&lt;br /&gt;
HTTP. This AbstractController handles the basic notion of controllers, actions, and action dispatching,&lt;br /&gt;
and not much else.&lt;br /&gt;
===Development===&lt;br /&gt;
====Rails generator system====&lt;br /&gt;
The new script/rails replaces all the scripts that used to be in the script directory. You do not run script/&lt;br /&gt;
rails directly though, the rails command detects it is being invoked in the root of a Rails application and&lt;br /&gt;
runs the script for you. Intended usage is:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Rails 2&lt;br /&gt;
! Rails 3&lt;br /&gt;
|-&lt;br /&gt;
| rails script/generate&lt;br /&gt;
| rails g&lt;br /&gt;
|-&lt;br /&gt;
| rails script/console&lt;br /&gt;
| rails c&lt;br /&gt;
|-&lt;br /&gt;
| rails script/server&lt;br /&gt;
| rails s&lt;br /&gt;
|-&lt;br /&gt;
| rails script/dbconsole&lt;br /&gt;
| rails db&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
====JS library support====&lt;br /&gt;
Rails 2 shipped with prototypejs as the default javascript library. However, Rails 3 makes it very easy&lt;br /&gt;
to pick a javascript library of choice, for example you can now use jquery as the base javascript library.&lt;br /&gt;
===Deployment &amp;amp; Dependency Management===&lt;br /&gt;
====Bundler====&lt;br /&gt;
Rails 2 required specifying gems in the environment.rb files, in the config block associated with the&lt;br /&gt;
run method as:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Rails::Initializer.run do |config|&lt;br /&gt;
config.gem &amp;quot;aws-s3&amp;quot;, :lib =&amp;gt; &amp;quot;aws/s3&amp;quot;&lt;br /&gt;
config.gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
config.gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
config.gem &amp;quot;acts-as-taggable-on&amp;quot;, :source =&amp;gt; &amp;quot;http://gemcutter.org&amp;quot;, :version =&amp;gt; '2.0.0.rc1'&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In contrast, Rails 3 uses a Gemfile in the application root to determine the gems you require for&lt;br /&gt;
your application to start. This Gemfile is processed by the Bundler, which then installs all your&lt;br /&gt;
dependencies. It takes a gem manifest file and is able to fetch, download, and install the gems and&lt;br /&gt;
all child dependencies specified in this manifest. It can manage any update to the gem manifest file&lt;br /&gt;
and update the bundle's gems accordingly. It also lets you run any ruby code in context of the bundle's&lt;br /&gt;
gem environment.&lt;br /&gt;
To use bundler, declare these dependencies in a file at the root of your application, called Gemfile. It&lt;br /&gt;
looks something like this:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
source &amp;quot;http://rubygems.org&amp;quot;&lt;br /&gt;
gem &amp;quot;aws-s3&amp;quot;, &amp;quot;1.2.0&amp;quot;&lt;br /&gt;
gem &amp;quot;fastercsv&amp;quot;&lt;br /&gt;
gem &amp;quot;chargify_api_ares&amp;quot;&lt;br /&gt;
gem &amp;quot;acts-as-taggable-on&amp;quot;, &amp;quot;~&amp;gt; 1.4.2&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using this Gemfile, the bundler looks for gems declared in the Gemfile at http://rubygems.org. You&lt;br /&gt;
can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the&lt;br /&gt;
sources.&lt;br /&gt;
Next, the bundler goes through the list of dependencies declared:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version 1.2.0 of aws-s3&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;any version of fastercsv and chargify_api_ares&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;version of acts-as-taggable-on that is &amp;gt;= 1.4.2 but &amp;lt; 1.5.0&amp;lt;\li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
After declaring the dependencies, you tell bundler to go get them:&lt;br /&gt;
&amp;lt;pre&amp;gt;$ bundle install&amp;lt;/pre&amp;gt;&lt;br /&gt;
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of&lt;br /&gt;
the required gems that meet the requirements you specified. Because all of the gems in your Gemfile&lt;br /&gt;
have dependencies of their own (and some of those have their own dependencies), running bundle&lt;br /&gt;
install on the Gemfile above will install quite a few gems. If any of the needed gems are already&lt;br /&gt;
installed, Bundler will use them. After installing any needed gems to your system, bundler writes a&lt;br /&gt;
snapshot of all of the gems and versions that it installed to Gemfile.lock.&lt;br /&gt;
On production servers, you can enable deployment mode:&lt;br /&gt;
$ bundle install --deployment&lt;br /&gt;
The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are&lt;br /&gt;
installed to vendor/bundle and the Gemfile.lock must be checked in and up to date before bundler is&lt;br /&gt;
run.&lt;br /&gt;
===Performance===&lt;br /&gt;
Rails Application object:&lt;br /&gt;
As part of the groundwork for supporting running multiple Rails applications in the same process,&lt;br /&gt;
Rails 3 introduces the concept of an Application object. An application object holds all the application&lt;br /&gt;
specific configurations and is very similar in nature to config/environment.rb from the previous versions&lt;br /&gt;
of Rails.&lt;br /&gt;
Each Rails application now must have a corresponding application object. The application object is&lt;br /&gt;
defined in config/application.rb. If you’re upgrading an existing application to Rails 3, you must add this&lt;br /&gt;
file and move the appropriate configurations from config/environment.rb to config/application.rb.&lt;br /&gt;
Rails 3 has improved performance by reducing overhead in following areas&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;General Controller Overhead&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Render Collections of Partials&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
Here is a comparison graph of performance between Rail 2.3 vs Rails 3.0 in the above mentioned&lt;br /&gt;
areas.&lt;br /&gt;
[[File:Blog 19big-1-.jpg|alt=Alt text|For basic operations]]&lt;br /&gt;
[[File:blog_19big.jpeg|alt=Alt text|For complex operations]]&lt;br /&gt;
==References==&lt;br /&gt;
# Rails 3 release notes: http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
# Yehuda Katz's posts on the merge of Rails and Merb: http://www.engineyard.com/blog/2009/rails-and-merb-merge-the-anniversary-part-1-of-6/.&lt;br /&gt;
# Nick Kallen's posts on AREL: http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/&lt;br /&gt;
# Bundler: http://gembundler.com/&lt;br /&gt;
# Pratik Naik's post on the changes in Active Record: http://m.onkey.org/active-record-query-interface&lt;br /&gt;
# Cookbook Application on Rails 2 discussed in class.&lt;/div&gt;</summary>
		<author><name>Hmalipa</name></author>
	</entry>
</feed>