Reputation: 621
I have a couple of classes that require a record/object created daily to be considered 'up-to-date' within my application.
Here are the methods I use to determine how recent the latest record was created.
class Resident < ActiveRecord::Base
.
.
.
def medical_record_expired?
if self.medical_record_forms.last.created_at::time <= 1.day.ago
true
else
false
end
# ((Time.current - self.medical_record_forms.last.created_at)/3600).to_i
end
def personal_care_log_expired?
if self.personal_care_forms.last.created_at::time <= 1.day.ago
true
else
false
end
# ((Time.current - self.personal_care_forms.last.created_at)/3600).to_i
end
end
My latest MedicalRecord's date is 12/17/2017
My latest PersonalCare's date is 12/21/2017
This is something that I built a while ago and I used to think that something simple such as object.created_at <= 1.day_ago
would return true for any date less than 1 day old.
I'm using postgresql 21 and below is my time zone config:
I can get the methods to work when running their internals in a console, so I figured this may be a time-zone conversion issue.
But I've tried out quite a few approaches that help with time-zone and other scenarios such as beginning_of_day, ::time and :to_i to calculate the n of hours that have passed, if less than 24 than the record is 'up-to-date', over 24hrs than it isn't.
I will also include my views in case I have something backwards there.
medical_record snippet:
<% if resident.medical_record_expired? %>
<td class="text-center move-row">
<label class="label label-danger">
Record outdated,
<%= link_to "<span class='pad-all text text-lg text-primary'>click here</span>".html_safe, new_resident_medical_record_form_path(resident.id) %>
to create.
</label>
</td>
<% else %>
<td class="text-center move-row">
<label class="label label-table label-success">Up to date!</label>
</td>
<% end %>
personal_care_logs snippet:
<% if resident.personal_care_log_expired? %>
<td class="text-center move-row">
<label class="label label-danger">
Record outdated,
<%= link_to "<span class='pad-all text text-lg text-primary'>click here</span>".html_safe, new_resident_personal_care_form_path(resident.id) %>
to create.
</label>
</td>
<% else %>
<td class="text-center move-row">
<label class="label label-table label-success">Up to date!</label>
</td>
<% end %>
I'm interested in learning what I have overlooked/misapplied that prevents me from reaching this end.
12/26/17 Update
Following the steps outlined below, I was able to determine that this problem exists for only one Resident and his nested-association, resident.personal_care_forms.last
. The last created_at was printing as being days ago (12/21), even though the latest record was created today (12/26).
When attempting to add new personal_care_forms
for other residents, the conditional pulls the record with the most recent created_at
date as expected.
I am unsure why the original resident.personal_care_forms.last
still does not pick up that I created a new record today as well. I can rebuild the db as this isn't a problem with production data.
Many thanks for the debug tip as it helped me reason through with greater ease.
Upvotes: 0
Views: 2045
Reputation: 5426
Simple self.personal_care_forms.last.created_at < 1.day.ago
should work and behave the same in the console and in the app.
Try adding logs to your Resident class like this (note that I also collapsed boolean returns) and see what you're getting:
class Resident < ActiveRecord::Base
.
.
.
def medical_record_expired?
puts "created_at: #{medical_record_forms.last.created_at} 1.day.ago: #{1.day.ago}"
self.medical_record_forms.last.created_at < 1.day.ago
end
def personal_care_log_expired?
puts "created_at: #{personal_care_forms.last.created_at} 1.day.ago: #{1.day.ago}"
self.personal_care_forms.last.created_at < 1.day.ago
end
end
Upvotes: 1