4
我想通过样本数据填充数据库,出于某种原因,我也想模拟created_at
。 这是我seeds.rb:模拟seeds.rb中的时间戳
9.downto(1) do |i|
product = Product.new(price: 99.99)
product.created_at = i.days.ago,
product.save!
end
在数据库中,rake db:seed
结果的样子,
---- 2012-03-03 16:50:30.316886000 Z- 1
,当我需要
2012-03-03 16:50:30.316886000 Z- 1
如何避免这些符号在结果中?
(DB:sqlite3的)
更新: 我发现,当我使用product.created_at = i.days.ago
, 回调(before_save
)created_at
是Array
:[date_value, 1]
。所以,我可以使用
before_save { self.created_at = self.created_at[0] }
,然后在数据库中的值将是正确的(不含),但使用回调似乎不是一个好办法。
'product.created_at =(Date.new - i)'为你工作吗? – ScottJShea 2012-03-10 18:10:09
感谢您的建议,但不,它不起作用。我有一些奇怪的价值('--- -4713-12-25-1') – evfwcqcg 2012-03-10 18:34:06