# Find conditions

**URL:** <https://www.ruby-forum.com/t/find-conditions/176485>\
**Category:** Rails\
**Created:** [October 1, 2009, 7:27am UTC](https://www.ruby-forum.com/t/find-conditions/176485 "2009-10-01T07:27:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [October 1, 2009, 7:27am UTC](https://www.ruby-forum.com/t/find-conditions/176485/1 "2009-10-01T07:27:35Z")

</div>

I’m rails a newbie:

I have a form setup where users selects datetime, the datetime serves  
the purpose of allowing the user to specify when the entry expires. In  
efforts to filter entrys that have an earlier datetime than now I am  
using the following:

def self.find\_active  
find(:all, :conditions =\> [‘expiration \> ?’, DateTime.now])

It does not filter out the expiration inputs that are further in advance  
than DateTime.now

Any help would be greatly appreciated 🙂

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [October 1, 2009, 9:46am UTC](https://www.ruby-forum.com/t/find-conditions/176485/2 "2009-10-01T09:46:03Z")

</div>

On Oct 1, 6:27 am, Greg K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m rails a newbie:
> 
> I have a form setup where users selects datetime, the datetime serves  
> the purpose of allowing the user to specify when the entry expires. In  
> efforts to filter entrys that have an earlier datetime than now I am  
> using the following:
> 
> def self.find\_active  
> find(:all, :conditions =\> [‘expiration \> ?’, DateTime.now])

You’ve got your condition back to front - that returns entries whose  
expiry is after now

Fred

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [October 1, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/find-conditions/176485/3 "2009-10-01T14:05:55Z")

</div>

Frederick C. wrote:

> On Oct 1, 6:27ï¿½am, Greg K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m rails a newbie:
> > 
> > I have a form setup where users selects datetime, the datetime serves  
> > the purpose of allowing the user to specify when the entry expires. ï¿½In  
> > efforts to filter entrys that have an earlier datetime than now I am  
> > using the following:
> > 
> > def self.find\_active  
> > ï¿½ ï¿½ find(:all, :conditions =\> [‘expiration \> ?’, DateTime.now])
> 
> You’ve got your condition back to front - that returns entries whose  
> expiry is after now

I don’t think he’s got it backwards – he’s looking for entries that  
haven’t yet expired.

> Fred

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [October 2, 2009, 2:57am UTC](https://www.ruby-forum.com/t/find-conditions/176485/4 "2009-10-02T02:57:58Z")

</div>

Rails Newb wrote:

> def self.find\_active  
> find(:all, :conditions =\> [‘expiration \> ?’, DateTime.now])

On a side note: Wouldn’t this be a good candidate for a named\_scope  
rather than a class method?

named\_scope :active, lamda { :conditions =\> [ ‘expiration \> ?’,  
DateTime.now ] }

or

named\_scope :active, lamda { :conditions =\> [ ‘expiration \<= ?’,  
DateTime.now ] }

or whatever conditions you determine are the right ones for your case.

P.S. This would be more conventional to the Rails framework:

named\_scope :active, lamda { :conditions =\> [ ‘expires\_at \> ?’,  
DateTime.now ] }

where expires\_at is a datetime column in the database.

---

<div class="post-metadata">

**Author:** ![railsnut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@railsnut](https://www.ruby-forum.com/u/railsnut)\
**Post date:** [October 1, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/find-conditions/176485/5 "2009-10-01T14:46:22Z")

</div>

2009/10/1 Greg K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m rails a newbie:
> 
> I have a form setup where users selects datetime, the datetime serves  
> the purpose of allowing the user to specify when the entry expires. Â In  
> efforts to filter entrys that have an earlier datetime than now I am

It is not completely clear from the above whether you are trying to  
select records with an expiry date greater than now or earlier than  
now.

> using the following:
> 
> def self.find\_active  
> Â Â find(:all, :conditions =\> [‘expiration \> ?’, DateTime.now])
> 
> It does not filter out the expiration inputs that are further in advance  
> than DateTime.now

What type of column (in the database) is expiration?

Colin
