[url]http://www.neeraj.name/blog/articles/800-override-automatic-updated_at-in-activerecord-rails[/url]
module ActiveRecord
class Base
def update_record_without_timestamping
class << self
alias_method :orig_record_timestamps, :record_timestamps
def record_timestamps; false; end
end
yield if block_given?
class << self
remove_method :record_timestamps
alias_method :record_timestamps, :orig_record_timestamps
end
end
end
end
本文介绍了一种在 ActiveRecord Rails 中覆盖自动更新_at字段的方法。通过修改Base类中的record_timestamps方法,可以实现在更新记录时不自动更新_at的时间戳。

被折叠的 条评论
为什么被折叠?



