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}
精彩评论