作者:Ryan Daigle 来源:JavaEye 酷勤网收集 2008-05-30
摘要
为了启用partial updates功能,在每个model中设置partial_updates = true,如果要在整个系统中启动这个功能,你需要修改environment.rb文件,或者更好的方式是在config/initializer设置。注意:现在config/initializers/new_rails_defaults.rb文件保存这个设置
系列文章汇总《Ruby on Rails 2.1新特性》
Rails2.1新特性中紧接着dirty objects功能就是Partial Updates部分更新能力,见ActiveRecord models to perform partial updates :它只保存被修改的属性。
例如:
Ruby代码 
- article = Article.find(:first)
- article.title #=> "Title"
- article.subject #=> "Edge Rails"
- # Update one of the attributes
- article.title = "New Title"
- # And only that updated attribute is persisted to the db
- article.save
- #=> "UPDATE articles SET title = 'New Title' WHERE id = 1"
article = Article.find(:first) article.title #=> "Title" article.subject #=> "Edge Rails" # Update one of the attributes article.title = "New Title" # And only that updated attribute is persisted to the db article.save #=> "UPDATE articles SET title = 'New Title' WHERE id = 1"
注意:你的updated_at/on魔法字段只有当未曾保存的属性需要持久化的时候才会被设置,如果持久化对象object没有被修改的属性,将没有任何SQL语句执行。
为了启用partial updates功能,在每个model中设置partial_updates = true,如果要在整个系统中启动这个功能,你需要修改environment.rb文件,或者更好的方式是在config/initializer设置:
- ActiveRecord::Base.partial_updates = true
ActiveRecord::Base.partial_updates = true
注意:现在config/initializers/new_rails_defaults.rb文件保存这个设置,所以如果你有这个文件,直接编辑就可以了。

