Ruby on Rails 2.2 发行笔记

Ruby on Rails 2.2 发行笔记

1. Infrastructure
Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.
1.1. 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 :
Official Rails i18 website
Finally. Ruby on Rails gets internationalized
Localizing Rails : Demo application
1.2. 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.

3. 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.
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
4. 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.意味着在内存里更少的RAILS拷贝可以处理更多的请求,导致更好的服务性能和更高的多核利用率。
To enable multithreaded dispatching in production mode of your application, add the following line in your config/environments/production.rb:
添加
config.threadsafe!
More information :
Thread safety for your Rails
Thread safety project announcement
Q/A: What Thread-safe Rails Means
5. 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.
5.1. 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 out of the box.
现在PostgreSQL支持,以后会扩展其他数据库。 The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.
Lead Contributor: Adam Wiggins
More information:
DDL Transactions
A major milestone for DB2 on Rails
5.2. 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.
development:
  adapter: mysql
  username: root
  database: sample_development
  pool: 10
  wait_timeout: 10
Lead Contributor: Nick Sieger
More information:
What's New in Edge Rails: Connection Pools
5.3. 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.
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 }})
在conditions里使用hashes来指定查询条件
More information:
What's New in Edge Rails: Easy Join Table Conditions
5.4. New Dynamic Finders
Two new sets of methods have been added to Active Record's dynamic finders family.
5.4.1. find_last_by_<attribute>
The find_last_by_<attribute> method is equivalent to Model.last(:conditions ⇒ {:attribute ⇒ value})
# Get the last user who signed up from London
User.find_last_by_city('London')
Lead Contributor: Emilio Tagua
5.4.2. find_by_<attribute>!
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.
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')
Lead Contributor: Josh Susser
5.5. Associations Respect Private/Protected Scope关联开始增加对Private/Protected的支持(不能通过关联调用对方的私有方法)
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: Adam Milligan
More information:
Rails 2.2 Change: Private Methods on Association Proxies are Private
5.6. 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
6. Action Controller
On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.
6.1. 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.
map.resources :publishers, :shallow => true do |publisher|
  publisher.resources :magazines do |magazine|
    magazine.resources :photos
  end
end
This will enable recognition of (among others) these routes:
/publishers/1           ==> publisher_path(1)
/publishers/1/magazines ==> publisher_magazines_path(1)
/magazines/2            ==> magazine_path(2)
/magazines/2/photos     ==> magazines_photos_path(2)
/photos/3               ==> photo_path(3)
Lead Contributor: S. Brent Faulkner
More information:
Rails Routing from the Outside In
What's New in Edge Rails: Shallow Routes

你可能感兴趣的:(db2,Ruby,Rails,ActiveRecord,jruby)