嘿,我有一个发现条件,这是一个有点长,我想让它更长。
到目前为止
:conditions => ["name LIKE ? OR description LIKE ?", "%#{params[:search_keyword]}%",
"%#{params[:search_keyword]}%"]它在模型中搜索属性:name或:description是否类似于文本框中的参数。现在我还想添加,以确保找到的条目是最新的。我已经有了
:conditions => ["start_date between ? and ? ", Date.today, @yearstime ]其中@yearstime是@yearstime = Date.today +365.day,所以基本上模型中从当前日期到一年后的所有内容
我想将这两个条件结合起来,但不确定其语法。我试着把它们放在一起。
:conditions => ["name LIKE ? OR description LIKE ? AND start_date between ? and ?",
"%#{params[:search_keyword]}%", "%#{params[:search_keyword]}%", Date.today, @yearstime ]但它仍然没有计算日期parameter....Any知道我做错了什么吗?
发布于 2010-07-07 09:17:12
看起来可能是运算符的顺序问题。试一试
:conditions => ["(name LIKE ? OR description LIKE ?) AND (start_date between ? and ?)",
"%#{params[:search_keyword]}%", "%#{params[:search_keyword]}%", Date.today, @yearstime ]AND运算符的优先级高于OR
参考资料:
发布于 2010-07-07 09:18:37
start_date是日期,还是date_time。可能是一个从一个到另一个的选角问题。
https://stackoverflow.com/questions/3191247
复制相似问题