【问题标题】:Rails -- Find with 3 conditions?Rails - 查找 3 个条件?
【发布时间】:2012-11-18 09:47:56
【问题描述】:

嘿,我的查找条件有点长,我想加长一点。

到目前为止

: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.days,所以基本上模型中从当前日期到一年后的所有内容

我想结合这两个条件,但不确定语法。我试着把它们放在一起。

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

但它仍然没有计算日期参数....知道我做错了什么吗?

【问题讨论】:

    标签: ruby-on-rails find conditional-statements


    【解决方案1】:

    看起来这可能是操作员的顺序问题。试试

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

    AND 运算符的优先级高于 OR

    参考:

    【讨论】:

      【解决方案2】:

      start_date 是日期还是 date_time。可能是从一个到另一个的选角问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-15
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        • 2022-10-13
        • 2012-08-03
        相关资源
        最近更新 更多