What is the Rails 3.1 of writing the code below:
named_scope :min_2_items_last_90_days, {
:include => { :orders => :order_items },
:conditions => ['orders.created_at >= ?', 90.days.ago],
:group => 'people.id',
:having => 'COUNT(order_items.id) >= 2'
}
While writing it as
is syntactically correct, it probably (like your original code) doesn’t do quite what you think.
In both cases the
90.days.agois evaluated once only, when the class is loaded, so the 90 days will always be 90 days before you app was last restarted. If you haven’t restart your app for 10 days you’d actually be looking at stuff created in the last 100 days. You won’t notice this in development because your source code is continuously being reloaded (and thus the90.daysis reevaluated).Instead you should do
which ensures that the conditions will be re-evaluated every time you use the scope.