`
woody_420420
  • 浏览: 41124 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

column_timestamp plugin

阅读更多

  有些时候,我们可能需要记录某些列的更新时间,类似于rails的timestamps。手工一一处理稍显繁琐,于是将其抽取出来,写成了一个简单的plugin--column_timestamp,自动记录一些简单列的更新时间。

  •   使用方法

  1.将column_timestamp放到plugins目录。

  2.在表中针对要记录更新时间的列建立一个新列:[column_name]_updated_at。

  3.或者,在migration中使用:column_timestamp => true参数。例如:

 def self.up
    create_table :my_table do |t|
    t.string :name
      t.string :status,:column_timestamp => true
      t.timestamps
    end
  end
  

  这样,rails在执行migration的时候就会自动生成"status_updated_at"列。

  4.OK。开始工作了。每当status的值改变,status_updated_at就会自动记录更新的时间。

  5.如果要停止记录XXX_update_at,只需要在相应的model调用suppress_column_timestamps,例如:

class MyModel < ActiveRecord::Base
  suppress_column_timestamps
  ...
end
  • 版本问题 

  在2.0.2中通过。如果要在2.1+跑,可以将column_timestamp/vendor删除。并且注释column_timestamp/init.rb中的

require File.join(File.dirname(__FILE__),"/vendor/dirty_attr/init")

 

2008.10.15  23:59 星期三

分享到:
评论
4 楼 woody_420420 2008-10-16  
wosmvp 写道

貌似所有Rails中源码都没有column_timestamp这个词……怎么就在migrate里出来了***_updated_at呢


这个我只是简单的扩展了一下migration中使用的方法,如下:
    def column(name, type, options = {})
      _original_column(name, type, options)
      if options[:column_timestamp]
        _original_column("#{name}_updated_at", :datetime)
      end
    end

所以,加上:column_timestamp参数就会自动生成×××_update_at列

wosmvp 写道

column_timestamp.rb
27行中 update_without_column_timestamps,亦没有定义,好像看魔术是的,查看源码,都不方便……

这个,建议查看下rails源代码中alias_method_chain方法。
http://www.iteye.com/topic/249594
3 楼 woody_420420 2008-10-16  
冉翔 写道
BS用rar打包滴。哇咔咔

真是BS无所不在,越BS越精彩~
现在社会都流行啥打包阿~我的winrar装了好几百年了
2 楼 wosmvp 2008-10-16  
   1. def self.up  
   2.    create_table :my_table do |t|  
   3.    t.string :name  
   4.      t.string :status,:column_timestamp => true  
   5.      t.timestamps  
   6.    end  
   7.  end  

貌似所有Rails中源码都没有column_timestamp这个词……怎么就在migrate里出来了***_updated_at呢

column_timestamp.rb
27行中 update_without_column_timestamps,亦没有定义,好像看魔术是的,查看源码,都不方便……
1 楼 冉翔 2008-10-16  
BS用rar打包滴。哇咔咔

相关推荐

Global site tag (gtag.js) - Google Analytics