Ruby on rails 更新Rails模型而不触及临时时间戳
我有一个Ruby on rails 更新Rails模型而不触及临时时间戳,ruby-on-rails,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,我有一个Tweet模型。我想更新模型属性,但不更新时间戳 我已经阅读/尝试了一些链接,但对我无效: 我试图覆盖第二个链接中提到的ActiveRecord::Base,方法是在lib目录中创建一个新文件active\u record\u Base\u patches.rb。但是,引擎表示,在Tweet模型中找不到该方法 因此,我尝试将不带时间戳的update\u record\u移动到Tweet模型。现在已找到该方法,但时间戳已更新 有什么简单的方法可以暂时禁用Rails中的时间戳吗? L
Tweet
模型。我想更新模型属性,但不更新时间戳
我已经阅读/尝试了一些链接,但对我无效:
ActiveRecord::Base
,方法是在lib
目录中创建一个新文件active\u record\u Base\u patches.rb
。但是,引擎表示,在Tweet
模型中找不到该方法
因此,我尝试将不带时间戳的update\u record\u移动到Tweet
模型。现在已找到该方法,但时间戳已更新
有什么简单的方法可以暂时禁用Rails中的时间戳吗?
Laravel 4有如下内容:
PS:我在Ruby 2.2.0上使用Ruby on Rails 4.2.3。您可以在模型/关注
中创建关注点,并用模型\u时间戳.rb
require 'active_support/concern'
module ModelTimestamp
extend ActiveSupport::Concern
module ClassMethods
def without_timestamps
old = ActiveRecord::Base.record_timestamps
ActiveRecord::Base.record_timestamps = false
begin
yield
ensure
ActiveRecord::Base.record_timestamps = old
end
end
end
end
然后在您的模型Tweet
addincludemodeltimstamp
class Tweet < ActiveRecord::Base
include ModelTimestamp
...
end
我试过你的解决方案,但没用。我注意到一个触发器:在一列中更新当前时间戳。在我取下扳机后,我再次尝试了你的解决方案,现在它正在工作。
tweet = Tweet.last
Tweet.without_timestamps do
tweet.update(attributes)
end