distance_of_time_in_words with ActiveRecord TimeWithZone

Kha*_*ash 3 ruby-on-rails-3

我正在尝试distance_of_time_in_wordscreated_atActiveRecord对象的列上使用(Rails 3).

但我明白了

ActiveSupport::TimeWithZone can't be coerced into Fixnum
Run Code Online (Sandbox Code Playgroud)

我打电话的时候

distance_of_time_in_words(@user.created_at)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Dyl*_*kow 5

你也需要提供to_time.假设你想知道"从现在开始多久":

<%= distance_of_time_in_words(@user.created_at, Time.now) %>
Run Code Online (Sandbox Code Playgroud)

或者,您可以这样做:

<%= distance_of_time_in_words_to_now(@user.created_at) %>
Run Code Online (Sandbox Code Playgroud)