Thomas Buckley
Thomas Buckley

Reputation: 6056

rails find created_on withing last 5 minutes

I have the following code:

@gameRequests = GameRequest.find(:first, :conditions => ["created_on >= ?", created_on])

I'd like to find the oldest record within the last 5 minutes. How can I subtract the 5 minutes from created_on to do this? I cant seem to find any examples.

Thanks

Upvotes: 0

Views: 2732

Answers (3)

Phil Jeffs
Phil Jeffs

Reputation: 153

Try this:

@gameRequests = GameRequest.find(:first, :conditions => ["created_at >= ?", Time.now - 5.minutes], :order => "created_at ASC")

Upvotes: 1

Srdjan Pejic
Srdjan Pejic

Reputation: 8202

The code you want is:

@gameRequest = GameRequest.first(:conditions => ["created_on >= ?", DateTime.now - 5.minutes])

BTW, are you sure the field is named created_on and not created_at? Also, .first gets only the first record, not all of them. For that, you need .all

Upvotes: 1

fl00r
fl00r

Reputation: 83680

gameRequests = GameRequest.find(:first, :conditions => ["created_on >= ?", created_on - 5.minute])

Upvotes: 1

Related Questions