【问题标题】:Multiple LIKE matches with Arel & MetaWhere与 Arel 和 MetaWhere 的多个 LIKE 匹配
【发布时间】:2010-11-30 03:16:59
【问题描述】:

我正在将使用带有 SearchLogic 的 Rails-2.3 编写的应用程序转换为带有 Arel 和 MetaWhere 的 Rails-3.0,我正在运行一个我不知道如何编写的操作。

旧代码是:

if params[:city] && params[:city].respond_to?(:each)
  users = users.person_address_city_like_any(params[:city])
end

这样做是针对 params[:city] 数组中的每个项目运行 LIKE 匹配。

当只有一个搜索词时,这在 MetaWhere 中很容易:

users = users.where(:person => { :address => { :city.matches => '%city1%' } })

但是我如何用任意数量的城市写这个?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 searchlogic arel meta-where


    【解决方案1】:

    试试:

    users = users.where(:person => { :address => { :city.matches_any => ['%city1%','%city2%'] } })

    【讨论】:

    • 谢谢。到目前为止,我对 MetaWhere 印象深刻。
    • 当条件来自字符串时,我怎么能做同样的事情:“person.address.city”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多