# Filter out or not showing event records that are in the past

**URL:** <https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940>\
**Category:** Rails\
**Created:** [July 11, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940 "2009-07-11T22:05:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 11, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/1 "2009-07-11T22:05:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 12, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/2 "2009-07-12T19:53:43Z")

</div>

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 J.

On Jul 11, 4:05 pm, kevin lee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 27, 2009, 4:02am UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/3 "2009-07-27T04:02:59Z")

</div>

I tweaked the sample code for my model and called MyModel.NameofScope  
(i.e. Event.upcoming) in the model. To my surprise, it has no effect.  
I also tried one or two other conditions. I looked at the log and there  
is nothing related to named\_scope. I use fixtures to load data into the  
database (SQLite3) but I don’t think it matters. My Rails version is  
2.1 So how do I go about troubleshooting this problem? Any advice?  
Thanks!

Matt J. wrote:

> 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 J.
> 
> On Jul 11, 4:05ï¿½pm, kevin lee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 27, 2009, 8:21am UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/4 "2009-07-27T08:21:25Z")

</div>

On Sun, Jul 26, 2009 at 7:02 PM, kevin  
lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tweaked the sample code for my model and called MyModel.NameofScope  
> (i.e. Event.upcoming) in the model. To my surprise, it has no effect.  
> I also tried one or two other conditions. I looked at the log and there  
> is nothing related to named\_scope. I use fixtures to load data into the  
> database (SQLite3) but I don’t think it matters. My Rails version is  
> 2.1 So how do I go about troubleshooting this problem? Any advice?

If your tests pass, perhaps they’re not doing what you think they are  
🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 29, 2009, 7:40pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/5 "2009-07-29T19:40:12Z")

</div>

I use ActiveScaffold in my project. I posted, perhaps stupidly, a  
question on AS help forum on whether it supports named\_scope and no one  
replies for days now. I also could hardly find named\_scope discussion  
on that forum.

Hassan S. wrote:

> On Sun, Jul 26, 2009 at 7:02 PM, kevin  
> lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I tweaked the sample code for my model and called MyModel.NameofScope  
> > (i.e. Event.upcoming) in the model. ï¿½To my surprise, it has no effect.  
> > I also tried one or two other conditions. ï¿½I looked at the log and there  
> > is nothing related to named\_scope. ï¿½I use fixtures to load data into the  
> > database (SQLite3) but I don’t think it matters. ï¿½My Rails version is  
> > 2.1 ï¿½So how do I go about troubleshooting this problem? ï¿½Any advice?
> 
> If your tests pass, perhaps they’re not doing what you think they are  
> 🙂
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [July 29, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/6 "2009-07-29T19:53:10Z")

</div>

On Wed, Jul 29, 2009 at 10:40 AM, kevin  
lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I use ActiveScaffold in my project. I posted, perhaps stupidly, a  
> question on AS help forum on whether it supports named\_scope

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,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [August 12, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/7 "2009-08-12T21:24:01Z")

</div>

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 S. wrote:

> On Wed, Jul 29, 2009 at 10:40 AM, kevin  
> lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I use ActiveScaffold in my project. ï¿½I posted, perhaps stupidly, a  
> > question on AS help forum on whether it supports named\_scope
> 
> 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,
> 
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [August 12, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/8 "2009-08-12T22:34:41Z")

</div>

On Wed, Aug 12, 2009 at 12:24 PM, kevin  
lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [August 17, 2009, 9:29pm UTC](https://www.ruby-forum.com/t/filter-out-or-not-showing-event-records-that-are-in-the-past/171940/9 "2009-08-17T21:29:29Z")

</div>

With your guidance, I have now seen named\_scope work in my simple app by  
invoking it in an action! Thanks!

Hassan S. wrote:

> On Wed, Aug 12, 2009 at 12:24 PM, kevin  
> lee[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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).
> 
> 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,
> 
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan
