# Conditional queries

**URL:** <https://www.ruby-forum.com/t/conditional-queries/77011>\
**Category:** Ferret\
**Created:** [November 23, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/conditional-queries/77011 "2006-11-23T12:07:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![camerony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@camerony](https://www.ruby-forum.com/u/camerony)\
**Post date:** [November 23, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/conditional-queries/77011/1 "2006-11-23T12:07:07Z")

</div>

Hi. I’m attempting to return a result set which filters Pages by their  
active state and their start/end date. Whilst the code I have at the  
moment is doing this without problem, some of the Page items may not  
have an end date set and I can’t see a way of conditionally doing the  
query so these are not excluded.

Perhaps showing some code will help explain better;

search\_controller.rb

```
  @results = Page.multi_search(
    "active:(true)
    *:(#{params[:s]})
    search_start_date:( <= #{Time.now.strftime("%Y%m%d")} )
    search_end_date:( >= #{Time.now.strftime("%Y%m%d")} )",
    [],
    {:offset => @offset, :limit => @limit}
  )

```

Page.rb

def search\_start\_date  
self.start\_at.strftime("%Y%m%d") if self.start\_at  
end

def search\_end\_date  
self.end\_at.strftime("%Y%m%d") if self.end\_at  
end

The problem being that I can’t figure out how to do the equivalent of:

```
    search_end_date:( IS NULL OR >= #{Time.now.strftime("%Y%m%d")} 

```

)",

Thanks for any help/advice,

Cam

---

<div class="post-metadata">

**Author:** ![camerony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@camerony](https://www.ruby-forum.com/u/camerony)\
**Post date:** [November 23, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/conditional-queries/77011/2 "2006-11-23T14:50:01Z")

</div>

On Thu, Nov 23, 2006 at 12:07:07PM +0100, Cameron Yule wrote:

> Hi. I’m attempting to return a result set which filters Pages by their  
> active state and their start/end date. Whilst the code I have at the  
> moment is doing this without problem, some of the Page items may not  
> have an end date set and I can’t see a way of conditionally doing the  
> query so these are not excluded.

[…]

> The problem being that I can’t figure out how to do the equivalent of:
> 
> ```
> search_end_date:( IS NULL OR >= #{Time.now.strftime("%Y%m%d")} 
> 
> ```
> 
> )",

indexing some special value like 99999999 for the end\_date of records  
not having an end data could to the trick.

Jens

–  
webit! Gesellschaft für neue Medien mbH [www.webit.de](http://www.webit.de)  
Dipl.-Wirtschaftsingenieur Jens Krämer removed\_email\_address@domain.invalid  
Schnorrstraße 76 Tel +49 351 46766 0  
D-01069 Dresden Fax +49 351 46766 66

---

<div class="post-metadata">

**Author:** ![camerony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@camerony](https://www.ruby-forum.com/u/camerony)\
**Post date:** [November 23, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/conditional-queries/77011/3 "2006-11-23T15:30:44Z")

</div>

Jens K. wrote:

> indexing some special value like 99999999 for the end\_date of records  
> not having an end data could to the trick.
> 
> Jens

Hi Jens, thanks for the reply. I’d considered something along those  
lines, but was curious if there was a neater method actually using the  
AAF querying syntax itself. Still, I think I’ll use your solution in the  
meantime.

Many thanks
