What Is New in Ruby on Rails 2.2
| Category | Highlights |
|---|---|
| New Features | Built-in i18n support, ETag/last-modified helpers, mailer layouts, memoization, each_with_object, delegate prefixes |
| Improvements | Thread-safe mode, connection pooling, shallow route nesting, method-array routes, :only/:except routing options, improved documentation guides |
| Bug Fixes | Association proxies respect private/protected scope, HTTP Accept header disabled by default, better benchmark reporting, HTTP-only cookies |
| Breaking Changes | Implicit local assignment in partials removed, country_select removed, allow_concurrency flag ignored, interpolation syntax %s/%d deprecated |
| Deprecations | String#chars → String#mb_chars, ActiveRecord::Errors.default_error_messages → I18n translations, %s/%d interpolation in i18n |
How does Rails 2.2 make internationalization easier?
Rails 2.2 ships with a first-class i18n framework that lets you store translations in YAML files and use the I18n.t helper throughout your app.
- All model error messages are now pulled from the i18n locale files.
- Helpers such as
link_toandform_forautomatically look up labels. - Deprecated interpolation syntax (
%s,%d) forces you to adopt the newer%{key}style.
In practice you add a config.i18n.default_locale = :de line to config/application.rb and place de.yml under config/locales.
What thread-safety and connection-pooling options does Rails 2.2 add?
Rails 2.2 introduces a production-ready thread-safe mode and configurable database connection pools.
# Enable thread-safe mode in production
config.threadsafe!
# Example pool configuration (config/database.yml)
development:
adapter: mysql
username: root
database: sample_development
pool: 10
wait_timeout: 10
- Thread-safe mode reduces memory pressure by sharing a single Rails instance across threads.
- Connection pools grow up to the
poolsize, defaulting to 5, and wait up towait_timeoutseconds for a free slot. - These settings are essential when deploying behind Puma, Thin, or JRuby servers that use native threads.
Which Active Record enhancements help migrations and joins in Rails 2.2?
Rails 2.2 adds transactional migrations, hash-based join conditions, and new dynamic finders.
# Transactional migration (PostgreSQL)
class AddIndexes < ActiveRecord::Migration
def self.up
transaction do
add_index :users, :email
add_index :orders, :created_at
end
end
end
- All steps inside a migration are wrapped in a DDL transaction; a failure rolls back the whole change.
- Join conditions can now be expressed as hashes, e.g.
Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false } }). - New dynamic finders
find_last_by_attributeandfind_by_attribute!reduce boilerplate and raiseActiveRecord::RecordNotFoundwhen appropriate.
How have routing and controller features changed in Rails 2.2?
Rails 2.2 refines routing with shallow nesting, method-array routes, and fine-grained resource generation.
# Shallow nesting example
map.resources :publishers, :shallow => true do |publisher|
publisher.resources :magazines do |magazine|
magazine.resources :photos
end
end
# Method array for collection route
map.resources :photos, :collection => { :search => [:get, :post] }
# Restrict generated actions
map.resources :products, :except => :destroy
- Shallow routes eliminate deeply-nested URLs while preserving RESTful semantics.
- Arrays of HTTP verbs let a single route respond to both GET and POST without duplication.
- The
:onlyand:exceptoptions cut down on generated routes, saving memory and improving request-dispatch speed. - Controllers now support
stale?andfresh_whenhelpers for automatic ETag/Last-Modified handling, returning 304 responses when appropriate.
Frequently Asked Questions
Does Rails 2.2 require Ruby 1.9 to run?
Rails 2.2 works with Ruby 1.8.7 and is prepared for Ruby 1.9, but it does not require it.
How do I enable ETag support in a controller?
Call stale?(:last_modified => record.updated_at, :etag => record) inside the action.
What command freezes gems into vendor/gems?
Run rake gems:unpack to copy all configured gems into the vendor/gems directory.
Can I use Thin as the default server in Rails 2.2?
Yes, script/server now supports Thin out of the box.
How do I turn the Accept header back on?
Set config.action_controller.use_accept_header = true in the environment config.
What is the new memoization syntax?
Extend ActiveSupport::Memoizable, define the method, then call memoize :method_name.