Question

I want to make a scope by name recent_flag which return products of last 40 days. i have a code like this, and thinking not perfect, is there any efficient way to make scope for my requirement.

scope :recent_flag, -> { where(product.created_at <= Time.now - 40.day)}

my database is postgresql.

Was it helpful?

Solution

I think You can modify your scope to

scope :recent_flag, -> { where(product.created_at > 40.days.ago)}

Note: Didn't tested.Try and let me know.

OTHER TIPS

Use:

scope :recent_flag, -> { where("created_at > ?",(Time.now - 40.days).beginning_of_day)}

Hope it helps :)

its the best way i thought.

scope :recent, -> { where(["created_at >= ?", Date.today - 14.days]) }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top