나는 내 응용 프로그램 내에서 '최신'으로 간주되기 위해 매일 생성되는 레코드/개체를 필요로하는 몇 가지 클래스가 있습니다.Rails 4.2 - 마지막 레코드의 #created_at가 1 일 이상 되었습니까?
다음은 최근 레코드가 얼마나 최근에 생성되었는지 확인하는 데 사용하는 방법입니다.
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
내 최신 MedicalRecord의 날짜가 내 최신 PersonalCare의 날짜가 2017년 12월 17일
이것은 내가 얼마 전에 내장 뭔가 내가있다 object.created_at <= 1.day_ago
과 같은 단순한 것이 1 일 미만의 모든 날짜에 대해 true를 리턴한다고 생각하는 데 사용됩니다. 나는 아래의 PostgreSQL (21)를 사용하고있어
내 시간대 설정입니다 :
나는 콘솔에서 자신의 내부를 실행할 때 방법이 작동을 얻을 수 있습니다, 그래서 나는이 시간이 될 수있다 생각 -zone 변환 문제.
그러나 시간대 및 다른 시나리오 (예 : beginning_of_day, :: time 및 to_i)를 사용하여 n 시간을 계산하는 방법을 시도해 보았습니다 (기록보다 24 시간 미만인 경우). 그것이 최신이 아닌 24 시간 이상입니다.
내가 거꾸로 뭔가를 가지고 있기 때문에 내 의견도 포함 할 것입니다.
medical_record는 니펫을 :
<% 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는 니펫을 :
<% 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 %>
가 나는이 끝에 도달에서 저를 방지하는 잘못 적용/간과 한 내용을 학습에 관심이 있어요. 아래에서 설명하는 단계를 다음 업데이트
12/26/17, 나는이 문제가 단지 하나의 주민과 그의 중첩 협회, resident.personal_care_forms.last
을 위해 존재 함을 확인할 수 있었다. 마지막으로 created_at은 오늘 (12/21)으로 인쇄되었지만 최신 레코드가 오늘 작성되었습니다. (12/26).
다른 주민을 위해 personal_care_forms
을 새로 추가하려고하면 조건부가 가장 최근의 created_at
날짜가 예상되는 레코드를 가져옵니다.
원래 resident.personal_care_forms.last
이 아직 내가 새 레코드를 만들었다 고 생각하지 않는 이유가 확실하지 않습니다. 프로덕션 데이터에 문제가 없으므로 db를 다시 작성할 수 있습니다.
디버그 팁을 주셔서 감사 드리며 더욱 쉽게 해결할 수있었습니다.