# Trying to find records that haven't been updated in 2 weeks

**URL:** <https://www.ruby-forum.com/t/trying-to-find-records-that-havent-been-updated-in-2-weeks/111707>\
**Category:** Rails\
**Created:** [August 9, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/trying-to-find-records-that-havent-been-updated-in-2-weeks/111707 "2007-08-09T23:51:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [August 9, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/trying-to-find-records-that-havent-been-updated-in-2-weeks/111707/1 "2007-08-09T23:51:11Z")

</div>

My app has some students that have attendances. I’m trying to find  
students who have not attended class in the last 2 weeks. Here is my  
code that is not working:

students = @school.students.active  
@missed\_attnd = []  
students.each do |student|  
unless student.attendances.empty?  
@missed\_attnd \<\< student if student.attendances.first.date \<=  
Date.today - 2.weeks  
end  
end

An attendance has a date column. Any idea how I can get this to work?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [August 10, 2007, 3:04am UTC](https://www.ruby-forum.com/t/trying-to-find-records-that-havent-been-updated-in-2-weeks/111707/2 "2007-08-10T03:04:24Z")

</div>

jko,

Databases can’t be assumed to retrieve rows in any specific order, so  
student.attendances.first cannot be assumed to be the most recent  
attendance.

In your loop, something like  
student.attendances.maximum(:date)  
should give you the last date they attended. Then you can compae in  
your loop.

However, note that this kind of loop is very expensive in database  
terms (a lookup for each student). I’m guessing you’ll be asking this  
question (ie. this query) more frequently than you’re updating the  
database. So it might make sense to optimise a little.

One approach is to add a column into attendances table called  
“most\_recent”. And in your model do this add filters to set  
“most\_recent” to true when adding a new record, and then setting the  
previous most\_recent to false.

Its more work on an insert, but a lot easier to manage a lookup.

missed\_students = Student.attendances.find(:conditions =\>  
‘students.active = true and attendances.most\_recent = true and  
attendances.date \<= ?’, Date.today - 2.weeks)

So one database operation and you have a list of students missing  
attendance.

Hope that helps a little,

Cheers, --Kip

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [August 11, 2007, 1:35am UTC](https://www.ruby-forum.com/t/trying-to-find-records-that-havent-been-updated-in-2-weeks/111707/3 "2007-08-11T01:35:47Z")

</div>

Thank you very much! I decided to go with the maximum(:date) because I  
will only use this functionality on the dashboard. If the performance  
is to slow then I’ll switch to your optimized suggestion.
