简体   繁体   English

Updated_at = created_at不起作用

[英]updated_at = created_at is not working

I am using mongoid on rails3. 我在rails3上使用蒙古包。 I am attempting to perform a very simple migration, but it is not working. 我正在尝试执行一个非常简单的迁移,但是它不起作用。 When it saves, it keeps saving the original value of a.updated_at, instead of a.created_at. 保存时,将继续保存a.updated_at的原始值,而不是a.created_at。 This is very perplexing - anyone has any ideas? 这非常令人困惑-有人有什么想法吗?

Answer.all.map{|a| a.updated_at = a.created_at; a.save;}

If you still have 如果你还有

Mongoid::Timestamps

included in your model, then, the callback after you save a document will automatically update the updated_at timestamp. 包含在模型中,那么保存文档后的回调将自动更新updated_at时间戳。 I guess that's why you keep seeing your updated_at always at the latest timestamp. 我想这就是为什么您总是在最新的时间戳上始终看到update_at的原因。

If you want a quick hack, you can remove that line from your model then run your migration. 如果要快速修改,可以从模型中删除该行,然后运行迁移。

Do remember to put that line back into your model again after the migration. 切记在迁移后再次将该线放回模型中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM