1
的整数余需要总结日期时间用于高速缓存期满目的的阵列,如日期时间总和返回类型错误:未在Heroku
# posts_controller.rb
@posts = Post.all
# index.html.erb
<% cache [:posts, :index, @posts.sum(&:updated_at)] do %>
# ...
哪我想将等于:
@posts.map(&:updated_at).reduce(:+)
问题是代码在本地正常工作(缓存和控制台测试),但在部署到heroku时返回生产中的以下错误:
TypeError: not an integer
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb:67:in `convert'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb:67:in `Rational'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/date_time/calculations.rb:67:in `since'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:116:in `rescue in since'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/time/calculations.rb:114:in `since'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/time_with_zone.rb:223:in `rescue in +'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/time_with_zone.rb:223:in `+'
from (irb):25:in `each'
from (irb):25:in `reduce'
from (irb):25
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Ruby版本是一样的(1.9.3)。 Rails版本是相同的(3.2.13)。
在此先感谢。
编辑
@posts.map(&:updated_at)
包含
[Thu, 11 Jul 2013 18:35:53 UTC +00:00, Thu, 11 Jul 2013 18:35:58 UTC +00:00,
Thu,11 Jul 2013 18:36:02 UTC +00:00, Thu, 11 Jul 2013 18:36:05 UTC +00:00,
Thu, 11 Jul 2013 18:36:09 UTC +00:00, Thu, 11 Jul 2013 18:36:14 UTC +00:00,
Thu, 11 Jul 2013 18:36:18 UTC +00:00]
你看过'updated_at'值吗?数据库中是否有NULL?你是否检查过大块“Post.all”以查找哪些记录导致问题? –
是的,我已经检查过。事情是,这是发生在不同的模型,每当我尝试添加多个updated_at时间。 我添加了数组内容,其实不是DateTime而是Time。我在某处读取了created_at和updated_at,它返回ActiveSupport :: TimeWithZone类,但是我得到了Times。 –
出于好奇,你为什么总结时间戳?看起来你使用它作为缓存键的一部分,但总和不能保证唯一性。 –