在Rails中手动设置updated_at [英] Manually set updated_at in Rails
问题描述
我将我的旧博客文章迁移到我的新Rails博客,我想要他们的 updated_at
属性匹配我的旧博客上的对应的值(不是他们的日期被迁移到我的新Rails博客)。
I'm migrating my old blog posts into my new Rails blog, and I want their updated_at
attribute to match the corresponding value on my old blog (not the date they were migrated into my new Rails blog).
我该如何做?当我手动设置 updated_at
时,它会被 before_save
回调覆盖。
How can I do this? When I set updated_at
manually it gets overridden by the before_save
callback.
推荐答案
如果是一次性事情,你可以打开或关闭 record_timestamps
。
If it's a one time thing you can turn record_timestamps
on or off.
ActiveRecord::Base.record_timestamps = false
#set timestamps manually
ActiveRecord::Base.record_timestamps = true
当我遇到这个问题与我的应用程序,我搜索了一下,它对我最有意义。这是一个初始化程序,我可以调用,我需要:
When I ran into this issue with my app, I searched around for a bit and this seemed like it made the most sense to me. It's an initializer that I can call where I need to:
module ActiveRecord
class Base
def update_record_without_timestamping
class << self
def record_timestamps; false; end
end
save!
class << self
def record_timestamps; super ; end
end
end
end
end
这篇关于在Rails中手动设置updated_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!