- 浏览: 145959 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
引用地址:
http://www.letrails.cn/archives/rails-update-record-without-timestamping/
Rails的save和 update方法默认都会将updated_at更新为当前时间,并且不允许手动设置,但是有些情况下,我们可能希望只更新记录的某个字段,而不希望 updated_at被更新,比如显示帖子的时候增加帖子的查看次数,这可以通过设置model的record_timestamps属性实现:
Post.record_timestamps=false
但是因为record_timestamps是一个类变量,因此改变这个值会影响到所有Post的保存,如果在同一时间有用户确实修改了Post的内容或者标题,updated_at也将不会更新。
Neeraj在这篇帖子里提供了一个方法,只修改当前对象的record_timestamps属性,在保存完后再进行恢复:
module ActiveRecord class Base def save_without_timestamping class << self def record_timestamps; false; end end save class << self def record_timestamps; super ; end end end end end
这段代码将打开当前对象的metaclass,并为当前对象定义一个record_timestamps方法来覆盖全局的设置,你可以将这段代码放到 initializer中,然后在不需要更新时间戳的时候调用save_without_timestamping来更新记录,就像下面这样:
post = Post.find(:id) post.view_count += 1 post.save_without_timestamping
上面一个实现是通过super方法恢复record_timestamps设置的,Neeraj还提供了一个使用remove_method的版本,我个人更喜欢这个版本:
module ActiveRecord class Base def save_without_timestamping class << self def record_timestamps; false; end end save class << self remove_method :record_timestamps end end end end
发表评论
-
Google App Engine and others
2010-09-15 11:27 765有用的技术文章 先记载 有空 研究下 さくっとRails ... -
基于jquery和mini_magick的图片裁剪
2010-09-15 11:07 1430引用网址 http://duyouhua1214.iteye ... -
ruby gem相关命令使用
2010-08-26 12:45 1558gem相关命令使用 1.显示gem的帮助和版本 gem – ... -
深入理解alias, alias_method和alias_method_chain
2010-08-06 17:14 949http://blackanger.blog.51cto.co ... -
Rails 3 特点
2010-08-06 16:38 868真的想留下来 http://railslove.com/w ... -
Fix N+1 Queries
2010-08-06 16:16 887http://rails-bestpractices.com/ ... -
query-reviewer sql explain and review in the page
2010-08-06 12:20 1296引用网址 http://github.com/dsboulde ... -
request-log-analyzer log分析工具
2010-08-06 12:03 1573参考网址: http://github.com/wvanber ... -
IE7 Bug导致Rails Session保存失败
2010-08-06 10:56 950http://www.letrails.cn/archives ... -
how to generate qrcode on ror
2010-02-04 16:27 8501. In your rails project, in ... -
rmagick ror 上 安装
2009-11-18 09:06 828rmgick 安装规范 ror很多人现在在用了,但是发现 ... -
Update Rails to 2.2.2 (Rails 2.2.2)
2009-03-08 14:53 958gem install -v=2.2.2 rails Suc ... -
Use helpers in controllers or models
2009-01-19 16:22 889引用http://snipplr.com/view/2505/ ... -
open-flash-chart
2008-11-07 14:07 2401http://pullmonkey.com/2008/7/23 ... -
rake db:migrate
2008-11-07 11:58 1250【ruby on rails】rake db:migrate ... -
rss maker
2008-11-07 11:43 1080http://gdgdlog.net/log/show/130 ... -
acts_as_authenticated
2008-11-07 11:35 1062ruby script/plugin install act ... -
validates
2008-11-07 11:24 1037先週ずっとインフルエンザでダウンしてました。 今日はvail ... -
rails で model の変更を監視するプラグイン
2008-11-07 11:12 1346acts_as_modified モデルの変更を監視して、各 ... -
exception_notification
2008-11-07 11:03 11551 applicattion.rb 配置 incl ...
相关推荐
rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...
《Rails之道》按照Rails的各个子系统进行组织编排,分别介绍了Rails的环境、初始过程、配置和日志记录,Rails的分配器、控制器、页面生成和路由,REST、资源和Rails,ActiveRecord的基础、关联、验证和高级技巧,...
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
约定大于配置 - rails railsrailsrails假定你想要做什么以及准备如何,而不是把任事情都放到配置文件中去。 假定你想要做什么以及准备如何,而不是把任事情都放到配置文件中去。 假定你想要做什么以及准备如何,...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
Ruby for Rails 英文原版, pdf格式 <br>本书是一部专门为Rails实践而写的经典Ruby著作,由四部分组成,共17章。第一部分讲述Ruby和Rails的编程环境。第二部分和第三部分与 Rails紧密联系,着重对Ruby这门语言...
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
rails指南 中文版
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...