DateTime sum返回TypeError:Heroku上不是整数

时间:2013-07-18 23:26:33

标签: ruby-on-rails ruby 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]

1 个答案:

答案 0 :(得分:0)

您可以尝试将缓存键设置为最近更新的帖子吗?

<% cache [:posts, :index, @posts.order(:updated_at).last] do %>