views:

23

answers:

2

Hey, I got a find condition that is a bit lengthy and I'd like make it lengthier.

SO far

:conditions =>  ["name LIKE ? OR description LIKE ?", "%#{params[:search_keyword]}%",
        "%#{params[:search_keyword]}%"]

Where it's searching the model for if the attribute :name or :description is like a param from a text box. Now I'd also like to add to make sure the entries found are current. I already have

:conditions => ["start_date between ? and ? ", Date.today, @yearstime ]

Where @yearstime is @yearstime = Date.today + 365.days so essentially everything thing in the model from the present date to a year from now

I'd like to combine both conditions but am unsure of the syntax. I tried to put it all together.

:conditions =>  ["name LIKE ? OR description LIKE ? AND start_date between ? and ?",
 "%#{params[:search_keyword]}%", "%#{params[:search_keyword]}%", Date.today, @yearstime ]

but it still isn't calculating for the date parameter....Any idea what I'm doing wrong?

+1  A: 

Looks like it's probably an order of operators issue. Try

:conditions =>  ["(name LIKE ? OR description LIKE ?) AND (start_date between ? and ?)",
 "%#{params[:search_keyword]}%", "%#{params[:search_keyword]}%", Date.today, @yearstime ]

The AND operator has a higher precedence than OR

Reference:

Jamie Wong
A: 

is start_date a date, or a date_time. might be a casting issue from one to the other

Jed Schneider