Art B
Art B

Reputation: 147

How to set option WHERE in offset method?

I have a string in code:

@card = Card.offset(rand(Card.count)).where("review_date"<=Time.now).first

I need 1 random record with date equal or less current date. but it show the error invalid date

Upvotes: 0

Views: 35

Answers (1)

Athar
Athar

Reputation: 3268

try this

@card = Card.where("review_date <= ?" , Time.now).limit(1).offset(rand(Card.count))

but here there is an extra overhead of count query first which will run as Card.count which you are using for offset.

Upvotes: 1

Related Questions