When writing it like
scope :min_2_items_last_90_days, where(...)
is syntactically correct, it probably (like your source code) is not doing what you think.
In both cases, the parameter 90.days.ago is evaluated only once when the class is loaded, so 90 days will always be 90 days before the last restart of the application. If you do not restart the application within 10 days, you will actually look for things created in the last 100 days. You will not notice this in development because your source code is constantly reloading (and therefore revaluing 90.days ).
Instead you should do
scope :min_2_items_last_90_days, lambda { where('orders.created_at >= ?', 90.days.ago).includes(...) ... }
which ensures that conditions are re-evaluated each time you use the area.
source share