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.
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.
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]) }