作者:Quake Wang 来源:JavaEye 酷勤网收集 2008-05-23
摘要
广受欢迎的Has Finder插件被集成到了2.1的代码中,不过换了种写法,使用named_scope;Rails2.1添加了类似Hibernate的检查对象是否变更机制,但是在使用上比Hibernate更加简洁,功能更加强大;在Dirty Objects的基础上,只更新对象变更属性的功能也加入了进来
Rails 2.1快出来了,和2.0相比它又添加了很多新特性,列举一下我认为值得关注的新特性:
1. Has Finder 功能
广受欢迎的Has Finder插件被集成到了2.1的代码中,不过换了种写法,使用named_scope:
Ruby代码 
- class User < ActiveRecord::Base
- named_scope :active, :conditions => {:active => true}
- named_scope :inactive, :conditions => {:active => false}
- named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
- end
- # 用法
- User.active # same as User.find(:all, :conditions => {:active => true})
- User.inactive # same as User.find(:all, :conditions => {:active => false})
- User.recent # same as User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
- # 还可以把他们组合起来组合用
- User.active.recent
class User < ActiveRecord::Base
named_scope :active, :conditions => {:active => true}
named_scope :inactive, :conditions => {:active => false}
named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end
# 用法
User.active # same as User.find(:all, :conditions => {:active => true})
User.inactive # same as User.find(:all, :conditions => {:active => false})
User.recent # same as User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
# 还可以把他们组合起来组合用
User.active.recent
你还可以传递参数:
- class User < ActiveRecord::Base
- named_scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] }
- end
- User.registered 7.days.ago # same as User.find(:all, :conditions => ['created_at > ?', 7.days.ago])
class User < ActiveRecord::Base
named_scope :registered, lambda { |time_ago| { :conditions => ['created_at > ?', time_ago] }
end
User.registered 7.days.ago # same as User.find(:all, :conditions => ['created_at > ?', 7.days.ago])
和对象关联定义类似,你还可以使用扩展:
- class User < ActiveRecord::Base
- named_scope :inactive, :conditions => {:active => false} do
- def activate
- each { |i| i.update_attribute(:active, true) }
- end
- end
- end
- # Re-activate all inactive users
- User.inactive.activate
class User < ActiveRecord::Base
named_scope :inactive, :conditions => {:active => false} do
def activate
each { |i| i.update_attribute(:active, true) }
end
end
end
# Re-activate all inactive users
User.inactive.activate
Rails2.1这个新特性可以极大地简化查询代码组合,达到更好的代码重用率,我们JavaEye的很多查询代码都可以从这个新特性上获益。
2. Dirty Objects
Rails2.1添加了类似Hibernate的检查对象是否变更机制,但是在使用上比Hibernate更加简洁,功能更加强大:
- article = Article.find(:first)
- article.changed? #=> false
- # 它还可以单独检查某个属性是否变更过
- # 使用 attr_name_changed? 方法
- article.title #=> "Title"
- article.title = "New Title"
- article.title_changed? #=> true
- # 使用 attr_name_was 方法获取变更前的值
- article.title_was #=> "Title"
- # 使用 attr_name_change 方法同时获取新旧2个值
- article.title_change #=> ["Title", "New Title"]
article = Article.find(:first) article.changed? #=> false # 它还可以单独检查某个属性是否变更过 # 使用 attr_name_changed? 方法 article.title #=> "Title" article.title = "New Title" article.title_changed? #=> true # 使用 attr_name_was 方法获取变更前的值 article.title_was #=> "Title" # 使用 attr_name_change 方法同时获取新旧2个值 article.title_change #=> ["Title", "New Title"]
这个新特性可以用来方便地做变更日志,状态改变监测等等多种用途,和原先需要自己写扩展代码相比要方便多了。
3. Partial Updates
在Dirty Objects的基础上,只更新对象变更属性的功能也加入了进来:
- article = Article.find(:first)
- article.title #=> "Title"
- article.subject #=> "Edge Rails"
- # 只更新一个属性
- article.title = "New Title"
- # 只有更新过的属性被执行了update操作
- article.save
- #=> "UPDATE articles SET title = 'New Title' WHERE id = 1"
article = Article.find(:first) article.title #=> "Title" article.subject #=> "Edge Rails" # 只更新一个属性 article.title = "New Title" # 只有更新过的属性被执行了update操作 article.save #=> "UPDATE articles SET title = 'New Title' WHERE id = 1"
如果要使用这个特性,需要设置:
- ActiveRecord::Base.partial_updates = true
ActiveRecord::Base.partial_updates = true
partial updates会提高更新的性能,我们JavaEye的一些使用update_all操作的代码都可以改成使用partial updates了。
http://ryandaigle.com/ 是一个国外的专门发布Rails新特性介绍的博客,除了上面提到的3点以外,还有一些特性值得关注:
来自:http://www.javaeye.com/topic/189171

