开发者

Iterate over records created_at dates and update with half of time from Time.now

开发者 https://www.devze.com 2023-02-06 13:18 出处:网络
I want to iterate over a fairly large collection of records in console, divide their date by half o开发者_如何学Pythonf the time since from Time.now and save it. So say records with created_at two mon

I want to iterate over a fairly large collection of records in console, divide their date by half o开发者_如何学Pythonf the time since from Time.now and save it. So say records with created_at two months ago would now be 1 month old, 1 day becomes 12 hours etc.

This doesn't work but just for example:

Log.all.each{|l| l.created_at = l.created_at - (Time.now - l.created_at * 0.5); l.save}


Try:

Log.all.each{|l| l.created_at = Time.at( l.created_at.to_f + (Time.now.to_f - l.created_at.to_f)/2 ); l.save}

Which should be the same as:

Log.all.each{|l| l.created_at = Time.at( (Time.now.to_f + l.created_at.to_f)/2 ); l.save}
0

精彩评论

暂无评论...
验证码 换一张
取 消