What Is New in Ruby on Rails 2.1
| Category | Highlights |
|---|---|
| New Features | Unified cache_store API, Action Mailer multipart & attachments, ActiveRecord named scopes, Expanded RESTful routing helpers |
| Improvements | Eager-loading performance, script/console enhancements, ActiveSupport core extensions (blank?, present?), better migration defaults |
| Bug Fixes | Association handling edge cases, routing wildcard bugs, Action Mailer header encoding fixes |
| Breaking Changes | Default cache store switched to :file_store, removal of the old script/plugin install command, stricter parameter handling in routes |
| Deprecations | Deprecated :conditions hash in find (encouraged use of where), old named_route syntax, legacy session store configuration |
How does the new cache store API affect my Rails 2.1 app?
It introduces a unified config.cache_store setting that lets you swap between memory, file, or memcached stores without touching your caching calls.
# config/environments/production.rb
config.cache_store = :mem_cache_store, "cache-01.example.com", "cache-02.example.com"
In practice you keep using Rails.cache.read/write as before, but you gain the ability to scale caching horizontally by pointing to a cluster of memcached nodes.
What are the Action Mailer enhancements in Rails 2.1?
Rails 2.1 adds native support for multipart/alternative emails and file attachments, making rich email composition straightforward.
class UserMailer < ActionMailer::Base
def welcome(user)
attachments['welcome.pdf'] = File.read('welcome.pdf')
mail(to: user.email,
subject: "Welcome",
content_type: "multipart/alternative") do |format|
format.text { render plain: "Welcome, #{user.name}" }
format.html { render html: "Welcome, #{user.name}
".html_safe }
end
end
end
This matters if your application sends invoices, newsletters, or any email that needs both plain-text and HTML versions.
How do named scopes improve ActiveRecord queries?
Named scopes let you define reusable, chainable query fragments directly on the model.
class Article < ActiveRecord::Base
named_scope :published, :conditions => { :status => 'published' }
named_scope :recent, :order => 'created_at DESC', :limit => 5
end
# usage
Article.published.recent
Most teams use this to keep controller code thin and to share common filters across the codebase.
What routing improvements should I be aware of when upgrading to Rails 2.1?
Named routes now accept optional parameters and resources gain explicit member and collection routes.
- Optional segments:
map.connect ':controller/:action/:id.:format'works without:format. - Member routes:
resources :photos do; member { get :preview }; end - Collection routes:
resources :photos do; collection { get :search }; end
Watch out for routes that previously relied on catch-all patterns; they may need to be tightened to avoid ambiguous matches.
Which deprecations and breaking changes require code changes?
The default cache store is now :file_store, so any custom cache initialization that assumed memory store must be updated.
The old script/plugin install command has been removed; use rails plugin install instead.
Deprecated :conditions hash in find calls encourages migration to the newer where syntax.
Frequently Asked Questions
Can I switch from the default file cache to memcached without changing my code?
You can change the cache store in the environment file and all existing Rails.cache calls will use memcached automatically.
Do I need to modify existing mailer templates to use the new multipart feature?
Only add a content_type: "multipart/alternative" line and provide both text and html blocks; existing templates continue to work as plain-text.
Is the named_scope syntax compatible with older Rails 2.0 applications?
Named scopes were introduced in Rails 2.1, so older apps must be upgraded before using them.
How do I migrate a find call that uses :conditions to the new where syntax?
Replace Article.find(:all, :conditions => { :status => 'draft' }) with Article.where(:status => 'draft').
Will my existing plugins still work after the script/plugin install command is removed?
Most plugins continue to work; you just install them with rails plugin install instead of the old command.
What is the simplest way to enable member routes for a resource?
Add member { get :preview } inside the resources block in routes.rb.