Conditional queries

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

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
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

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