<commit_before>Ruby on Rails 2.2 Release Notes
===============================

Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the link:http://github.com/rails/rails/commits/master[list of commits] in the main Rails repository on GitHub.

Along with Rails, 2.2 marks the launch of the link:http://guides.rubyonrails.org/[Ruby on Rails Guides], the first results of the ongoing link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]. This site will deliver high-quality documentation of the major features of Rails.

== Infrastructure

Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.

=== Internationalization

Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).

* Lead Contributors: Rails i18 Team
* More information :
  - link:http://rails-i18n.org[Official Rails i18 website]
  - link:http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized[Finally. Ruby on Rails gets internationalized]
  - link:http://i18n-demo.phusion.nl[Localizing Rails : Demo application]

=== Compatibility with Ruby 1.9 and JRuby

Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.

== Documentation

The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the link:http://guides.rubyonrails.org/[Ruby on Rails Guides] project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:

* Getting Started with Rails
* Rails Database Migrations
* Active Record Associations
* Active Record Finders
* Layouts and Rendering in Rails
* Action View Form Helpers
* Rails Routing from the Outside In
* Basics of Action Controller
* Rails Caching
* Testing Rails Applications
* Securing Rails Applications
* Debugging Rails Applications
* Benchmarking and Profiling Rails Applications
* The Basics of Creating Rails Plugins

All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.

If you want to these generate guides locally, inside your application:

[source, ruby]
-------------------------------------------------------
rake doc:guides
-------------------------------------------------------

This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.

* Lead Contributors: link:http://guides.rails.info/authors.html[Rails Documentation Team]
* Major contributions from link:http://advogato.org/person/fxn/diary.html[<NAME>] and link:http://izumi.plan99.net/blog/[Hongli Lai].
* More information:
 - link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]
 - link:http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch[Help improve Rails documentation on Git branch]

== Better integration with HTTP : Out of the box ETag support

Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.

[source, ruby]
-------------------------------------------------------
class ArticlesController < ApplicationController
  def show_with_respond_to_block
    @article = Article.find(params[:id])

    # If the request sends headers that differs from the options provided to stale?, then
    # the request is indeed stale and the respond_to block is triggered (and the options
    # to the stale? call is set on the response).
    #
    # If the request headers match, then the request is fresh and the respond_to block is
    # not triggered. Instead the default render will occur, which will check the last-modified
    # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
    # of rendering the template.
    if stale?(:last_modified => @article.published_at.utc, :etag => @article)
      respond_to do |wants|
        # normal response processing
      end
    end
  end

  def show_with_implied_render
    @article = Article.find(params[:id])

    # Sets the response headers and checks them against the request, if the request is stale
    # (i.e. no match of either etag or last-modified), then the default render of the template happens.
    # If the request is fresh, then the default render will return a "304 Not Modified"
    # instead of rendering the template.
    fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
  end
end
-------------------------------------------------------

== Thread Safety

The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.

To enable multithreaded dispatching in production mode of your application, add the following line in your +config/environments/production.rb+:

[source, ruby]
-------------------------------------------------------
config.threadsafe!
-------------------------------------------------------

* More information :
 - link:http://m.onkey.org/2008/10/23/thread-safety-for-your-rails[Thread safety for your Rails]
 - link:http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core[Thread safety project announcement]
 - link:http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html[Q/A: What Thread-safe Rails Means]

== Active Record

There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.

=== Transactional Migrations

Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by +rake db:migrate:redo+ after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported *on PostgreSQL only*. The code is extensible to other database types in the future.

* Lead Contributor: link:http://adam.blog.heroku.com/[<NAME>s]
* More information:
 - link:http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/[DDL Transactions]

=== Connection Pooling

Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a +pool+ key to your +database.yml+ to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a +wait_timeout+ that defaults to 5 seconds before giving up. +ActiveRecord::Base.connection_pool+ gives you direct access to the pool if you need it.

[source, ruby]
-------------------------------------------------------
development:
  adapter: mysql
  username: root
  database: sample_development
  pool: 10
  wait_timeout: 10
-------------------------------------------------------

* Lead Contributor: link:http://blog.nicksieger.com/[<NAME>]
* More information: 
 - link:http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools[What's New in Edge Rails: Connection Pools]

=== Hashes for Join Table Conditions

You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.

[source, ruby]
-------------------------------------------------------
class Photo < ActiveRecord::Base
  belongs_to :product
end

class Product < ActiveRecord::Base
  has_many :photos
end

# Get all products with copyright-free photos:
Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
-------------------------------------------------------

* More information:
 - link:http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions[What's New in Edge Rails: Easy Join Table Conditions]

=== New Dynamic Finders

Two new sets of methods have been added to Active Record's dynamic finders family.

==== find_last_by_<attributes>

The +find_last_by_<attribute>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
	
[source, ruby]
-------------------------------------------------------
# Get the last user who signed up from London
User.find_last_by_city('London')
-------------------------------------------------------

* Lead Contributor: link:http://www.workingwithrails.com/person/9147-emilio-tagua[<NAME>]

==== find_by_<attributes>! 
	
The new bang! version of +find_by_<attribute>! is equivalent to  +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
	
[source, ruby]
-------------------------------------------------------
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')
-------------------------------------------------------

* Lead Contributor: link:http://blog.hasmanythrough.com[<NAME>usser]

=== Associations Respect Private/Protected Scope

Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) +@user.account.private_method+ would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use +@user.account.send(:private_method)+ (or make the method public instead of private or protected). Please note that if you're overriding +method_missing+, you should also override +respond_to+ to match the behavior in order for associations to function normally.

* Lead Contributor: <NAME>

=== Other ActiveRecord Changes

* +rake db:migrate:redo+ now accepts an optional VERSION to target that specific migration to redo
* Set +config.active_record.timestamped_migrations = false+ to have migrations with numeric prefix instead of UTC timestamp. 
* Counter cache columns (for associations declared with +:counter_cache => true+) do not need to be initialized to zero any longer.
* +ActiveRecord::Base.human_name+ for an internationalization-aware humane translation of model names

== Action Controller

On the controller side, there are a couple of changes that will help tidy up your routes. 

=== Shallow Route Nesting

Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with - but you _can_ supply more information. 

[source, ruby]
-------------------------------------------------------
map.resources :publishers, :shallow => true do |publisher|<commit_msg>2.2 Relnotes: more info on association proxies<commit_after>Ruby on Rails 2.2 Release Notes
===============================

Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the link:http://github.com/rails/rails/commits/master[list of commits] in the main Rails repository on GitHub.

Along with Rails, 2.2 marks the launch of the link:http://guides.rubyonrails.org/[Ruby on Rails Guides], the first results of the ongoing link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]. This site will deliver high-quality documentation of the major features of Rails.

== Infrastructure

Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.

=== Internationalization

Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).

* Lead Contributors: Rails i18 Team
* More information :
  - link:http://rails-i18n.org[Official Rails i18 website]
  - link:http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized[Finally. Ruby on Rails gets internationalized]
  - link:http://i18n-demo.phusion.nl[Localizing Rails : Demo application]

=== Compatibility with Ruby 1.9 and JRuby

Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.

== Documentation

The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the link:http://guides.rubyonrails.org/[Ruby on Rails Guides] project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:

* Getting Started with Rails
* Rails Database Migrations
* Active Record Associations
* Active Record Finders
* Layouts and Rendering in Rails
* Action View Form Helpers
* Rails Routing from the Outside In
* Basics of Action Controller
* Rails Caching
* Testing Rails Applications
* Securing Rails Applications
* Debugging Rails Applications
* Benchmarking and Profiling Rails Applications
* The Basics of Creating Rails Plugins

All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.

If you want to these generate guides locally, inside your application:

[source, ruby]
-------------------------------------------------------
rake doc:guides
-------------------------------------------------------

This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.

* Lead Contributors: link:http://guides.rails.info/authors.html[Rails Documentation Team]
* Major contributions from link:http://advogato.org/person/fxn/diary.html[<NAME>] and link:http://izumi.plan99.net/blog/[Hongli Lai].
* More information:
 - link:http://hackfest.rubyonrails.org/guide[Rails Guides hackfest]
 - link:http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch[Help improve Rails documentation on Git branch]

== Better integration with HTTP : Out of the box ETag support

Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.

[source, ruby]
-------------------------------------------------------
class ArticlesController < ApplicationController
  def show_with_respond_to_block
    @article = Article.find(params[:id])

    # If the request sends headers that differs from the options provided to stale?, then
    # the request is indeed stale and the respond_to block is triggered (and the options
    # to the stale? call is set on the response).
    #
    # If the request headers match, then the request is fresh and the respond_to block is
    # not triggered. Instead the default render will occur, which will check the last-modified
    # and etag headers and conclude that it only needs to send a "304 Not Modified" instead
    # of rendering the template.
    if stale?(:last_modified => @article.published_at.utc, :etag => @article)
      respond_to do |wants|
        # normal response processing
      end
    end
  end

  def show_with_implied_render
    @article = Article.find(params[:id])

    # Sets the response headers and checks them against the request, if the request is stale
    # (i.e. no match of either etag or last-modified), then the default render of the template happens.
    # If the request is fresh, then the default render will return a "304 Not Modified"
    # instead of rendering the template.
    fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
  end
end
-------------------------------------------------------

== Thread Safety

The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.

To enable multithreaded dispatching in production mode of your application, add the following line in your +config/environments/production.rb+:

[source, ruby]
-------------------------------------------------------
config.threadsafe!
-------------------------------------------------------

* More information :
 - link:http://m.onkey.org/2008/10/23/thread-safety-for-your-rails[Thread safety for your Rails]
 - link:http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core[Thread safety project announcement]
 - link:http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html[Q/A: What Thread-safe Rails Means]

== Active Record

There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.

=== Transactional Migrations

Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by +rake db:migrate:redo+ after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported *on PostgreSQL only*. The code is extensible to other database types in the future.

* Lead Contributor: link:http://adam.blog.heroku.com/[<NAME>gins]
* More information:
 - link:http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/[DDL Transactions]

=== Connection Pooling

Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a +pool+ key to your +database.yml+ to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a +wait_timeout+ that defaults to 5 seconds before giving up. +ActiveRecord::Base.connection_pool+ gives you direct access to the pool if you need it.

[source, ruby]
-------------------------------------------------------
development:
  adapter: mysql
  username: root
  database: sample_development
  pool: 10
  wait_timeout: 10
-------------------------------------------------------

* Lead Contributor: link:http://blog.nicksieger.com/[<NAME>]
* More information: 
 - link:http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools[What's New in Edge Rails: Connection Pools]

=== Hashes for Join Table Conditions

You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.

[source, ruby]
-------------------------------------------------------
class Photo < ActiveRecord::Base
  belongs_to :product
end

class Product < ActiveRecord::Base
  has_many :photos
end

# Get all products with copyright-free photos:
Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
-------------------------------------------------------

* More information:
 - link:http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions[What's New in Edge Rails: Easy Join Table Conditions]

=== New Dynamic Finders

Two new sets of methods have been added to Active Record's dynamic finders family.

==== find_last_by_<attributes>

The +find_last_by_<attribute>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
	
[source, ruby]
-------------------------------------------------------
# Get the last user who signed up from London
User.find_last_by_city('London')
-------------------------------------------------------

* Lead Contributor: link:http://www.workingwithrails.com/person/9147-emilio-tagua[<NAME>]

==== find_by_<attributes>! 
	
The new bang! version of +find_by_<attribute>! is equivalent to  +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
	
[source, ruby]
-------------------------------------------------------
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')
-------------------------------------------------------

* Lead Contributor: link:http://blog.hasmanythrough.com[<NAME>]

=== Associations Respect Private/Protected Scope

Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) +@user.account.private_method+ would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use +@user.account.send(:private_method)+ (or make the method public instead of private or protected). Please note that if you're overriding +method_missing+, you should also override +respond_to+ to match the behavior in order for associations to function normally.

* Lead Contributor: <NAME>
* More information:
 - link:http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/[Rails 2.2 Change: Private Methods on Association Proxies are Private]

=== Other ActiveRecord Changes

* +rake db:migrate:redo+ now accepts an optional VERSION to target that specific migration to redo
* Set +config.active_record.timestamped_migrations = false+ to have migrations with numeric prefix instead of UTC timestamp. 
* Counter cache columns (for associations declared with +:counter_cache => true+) do not need to be initialized to zero any longer.
* +ActiveRecord::Base.human_name+ for an internationalization-aware humane translation of model names

== Action Controller

On the controller side, there are a couple of changes that will help tidy up your routes. 

=== Shallow Route Nesting

Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with - but you _can_ supply more information. 

[source, ruby]
-------------------------------------------------------
map.resources :publishers, :shallow => true do |publisher|
{'fraction_non_alphanumeric': 0.14329980050087016, 'fraction_numerical': 0.009423150388386604, 'mean_word_length': 3.2343637670740475, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644219', 'n_tokens_mistral': 6313, 'n_tokens_neox': 5802, 'n_words': 2643}