简体   繁体   中英

How to find all database rows with a time AFTER a specific time

I have a "last_action" column in my users table that updates a unix timestamp every time a user takes an action. I want to select all users in my users table who have made an action on the site after a specific time (probably 15 minutes, the goal here is to make a ghetto users online list).

I want to do something like the following...

time = Time.now.to_i - 900 # save the timestamp 15 minutes ago in a variable 
User.where(:all, :where => :last_action > time)

What's the best way to do this? Is there a way of doing it without using any raw SQL?

Maybe this will work?

User.where("users.last_action > ?", 15.minutes.ago)

Try this:

time = Time.now.to_i - 900
User.where("last_action > #{time}")

There might be a nicer/safer syntax to put variable arguments in to the where clause, but since you are working with integers (timestamps) this should work and be safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM