我在尝试使用' time_ago_in_words'时遇到了一个奇怪的错误。在rails中的conversation.messages.first(created_at)上运行:
<% conversations.each do |conversation| %>
<% user_status = current_user.id == conversation.sender.id ? conversation.recipient : conversation.sender %>
<li>
<%= gravatar_for(user_status, size: 50) %>
<%= link_to user_status.email, conversation_messages_path(conversation),
remote: true,
class: "conversation-names #{'current-link' if conversation == conversations.first}" %>
<span class="delete-message"><%= link_to do %><i class="fa fa-times"></i><% end %></span>
<p><%= truncate(conversation.messages.first.try(:body)) %><%= time_ago_in_words conversation.messages.first.try(:created_at) %></p>
</li>
<% end %>
未定义的方法`&gt;&#39;为零:NilClass。
答案 0 :(得分:1)
Iceman击中了头部。
在<li>
conversation.message
中确保.first
存在之前,先致电.first
;否则nil
将返回SELECT COUNT(DISTINCT pets.id),locations.attr -> 'country' as country
FROM pets,photos,locations
WHERE photos.pet_id = pets.id
AND photos.location_id = locations.id
GROUP BY pets.id,locations.attr -> 'country';
。