我试图通过rake任务手动编辑"updated_at“字段。
如下所示:
task :campaigns_updated_at_recovery => :environment do
Dir.foreach('db/raw-data/campaigns/') do |json|
next if json == '.' or json == '..'
file = File.read('db/raw-data/campaigns/'+json)
data_hash = JSON.parse(file)
#p data_hash["_id"]
thisCampaign = Campaign.find(data_hash["_id"])
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
thisCampaign.save
end
end但是,当我运行此任务时,updated_at日期要么没有更改,要么更新到今天的日期。我错过了什么?
--顺便说一句,我使用的是蒙古人,而不是ActiveRecord
发布于 2018-04-24 08:05:53
updated_at是由mongoid本身在回调中更新的。
你有两个解决方案来解决这个问题。
最简单的解决方案是使用set直接更改值,而不触发任何回调:
thisCampaign.set(channels: thisCampaign['channels'])更灵活的解决方案是降低到驱动程序级别。其基本思想是:
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {
updated_at: yourDate
}
})考虑到您的示例,您首先需要获得完整的文档。
thisCampaign = Campaign.collection.find(_id: data_hash["_id"]).first
if thisCampaign
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
end
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {channels: thisCampaign["channels"]}
})
endhttps://stackoverflow.com/questions/49986591
复制相似问题