# Find all users who haven't logged in for a while

**URL:** <https://www.ruby-forum.com/t/find-all-users-who-havent-logged-in-for-a-while/116759>\
**Category:** Rails\
**Created:** [October 9, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/find-all-users-who-havent-logged-in-for-a-while/116759 "2007-10-09T14:25:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [October 9, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/find-all-users-who-havent-logged-in-for-a-while/116759/1 "2007-10-09T14:25:38Z")

</div>

Hi All,

In my application I have users that have a ‘last\_login’ attribute. I’m  
trying do a find for all users that have not logged in for over a  
month but I’m having a little trouble figuring out how to do this with  
activerecord. Does anyone know how to do this, or do I have to use SQL?

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [October 9, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/find-all-users-who-havent-logged-in-for-a-while/116759/2 "2007-10-09T14:37:22Z")

</div>

On Oct 9, 2007, at 7:24 , jeroen wrote:

> I’m  
> trying do a find for all users that have not logged in for over a  
> month but I’m having a little trouble figuring out how to do this with  
> activerecord.

# sql version

sql = \<\<-SQL  
select \*  
from users  
where last\_login \< current\_timestamp - interval ‘1 month’  
SQL  
users\_on\_vacation = Users.find\_by\_sql(sql)

# first ActiveRecord version

users\_on\_vacation = Users.find(:all, :conditions =\> [%(last\_login \<  
current\_timestamp - interval ‘1 month’)])

# second ActiveRecord version

users\_on\_vacation = Users.find(:all, :conditions =\> [%(last\_login  
\< ?), 1.month.ago])

Michael G.  
grzm seespotcode net
