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。 –
出於好奇,你爲什麼總結時間戳?看起來你使用它作爲緩存鍵的一部分,但總和不能保證唯一性。 –