Ruby on Rails的:的updated_at - 设置为0的创造 [英] Ruby on Rails: updated_at - set as 0 on creation
问题描述
当我创建查询
表中的一个新的记录,我想的updated_at
(时间戳)是0000- 00-00 00:00:00 - 而不是当前的时间戳,如Ruby on Rails的设置默认
When I create a new record on query
table, I want updated_at
(timestamp) to be '0000-00-00 00:00:00' - and not the current timestamp, as Ruby on Rails sets by default.
MySQL的领域:
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
不过,Ruby on Rails的生成此SQL查询:
However, Ruby on Rails generates this sql query:
INSERT INTO `queries` (`updated_at`) VALUES ('2011-01-04 17:40:15')
我该如何解决这个问题?
How do I fix this?
推荐答案
滑轨套的updated_at
为当前时间,因为,这就是当记录最近一次更新。它把更新作为创作。如果您要检查,如果事情曾经被更新,请检查其的updated_at
和 created_at
是相同的。
Rails sets updated_at
to the current time because, well, thats when the record was most recently updated. It sees an update as a creation. If you want to check if something has ever been updated, check to see if its updated_at
and created_at
are the same.
否则,你应该能够改变迫使在插入属性插入值。
Otherwise, you should be able to change the value inserted by forcing the attribute upon insertion.
Post.new(@ params.merge({:的updated_at =>日期时间::新})
或者,在你的模型,设置了 before_create
方法 self.updated_at =日期时间::新
。
or, in a before_create
method on your model, set self.updated_at = DateTime::new
.
这篇关于Ruby on Rails的:的updated_at - 设置为0的创造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!