My app shows a list of upcoming events. Without having to modify the database frequently to keep up with the current date, I would like to be able to keep the events that are now in the past from being shown. Can I do this using filter? Any example or link to a tutorial would be appreciated. Thanks!
Try this in your event model:
named_scope :upcoming, lambda { { :conditions => ['happens_at > ?', Time.now] } }
I've assumed that your scheduled date is named 'happens_at'; tweak as needed.
--Matt Jones
If your tests pass, perhaps they're not doing what you think they are
I know zip about ActiveScaffold, but I imagine you would get more useful help if you posted your actual model code and the controller code where you're calling your named scope.
Though -- a suggestion -- trying things out in the console is often an effective way to debug a problem...
FWIW,
I have generated a simple event app to learn and try to debug named_scope. But still have problem. My problem is still have not figure out how to call the named_scope (i.e. Event.upcoming). Not knowing exactly how, I tried to invoked it in various spots in the controller and also the view to no avail (and also used script/console). The online articles about named_scope I found just invoke it (or listed the usage line) below the model class. It looks so simple to understand but I guess I don't have enough brain power. Please help me out by giving a more concrete example. Thanks!
Hassan Schroeder wrote:
Ex: named_scopes in a model:
named_scope :mode, lambda { |mode| { :conditions => ['mode = ?', mode] } } named_scope :available, lambda { |current_user| { :conditions => [ " permission = ? OR user_id = ?" , 'public', current_user.id ] } }
:: and in the controller:
@notes = Note.available(current_user).mode params[:mode]
:: which also demonstrates scope chaining, FWIW.
HTH,
With your guidance, I have now seen named_scope work in my simple app by invoking it in an action! Thanks!
Hassan Schroeder wrote: