与 Arel & 的多次 LIKE 匹配元凡

发布于 2024-10-05 06:26:49 字数 654 浏览 0 评论 0原文

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

旧代码是:

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%' } })

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

I'm transitioning an application written in Rails-2.3 with SearchLogic to Rails-3.0 with Arel and MetaWhere, and I'm running into an operation that I don't know how the write.

The old code was:

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

what this did was run a LIKE match against each item in the params[:city] array.

This is easy enough in MetaWhere when there's only one search term:

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

but how would I write this with an arbitrary number of cities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2024-10-12 06:26:49

尝试:

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

Try:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文