I get the following error, and thought by using the .nil? method I could avoid getting an error by identifying the exception. But I don't.
Line 40 shows I am getting an error...it seems to think that contact.latest_event is nil. But shouldn't .nil? help me avoid getting the error? Thanks...!
ActionView::TemplateError (undefined method `<=>' for nil:NilClass) on line #40
of app/views/companies/show.html.erb:
37: <p>
38: <%= full_name(contact) %>, <%= contact.status %><%= contact.titl
e %>,
39: <span class='date_added'>added <%= contact.date_entered %>
40: <% if !contact.latest_event.nil?%>
41: last event: <%= contact.latest_event.date_sent %>
42: <% end %>
43: </span>
Here is latest_event:
def latest_event
[contact_emails, contact_calls, contact_letters].map do |assoc|
assoc.first(:order => 'date_sent DESC')
end.compact.sort_by { |e| e.date_sent }.last
end
I guess it's possible none of the Models contact_emails, for example, have been done...but what do I do if there aren't any that exist?