与 Arel & 的多次 LIKE 匹配元凡
我正在将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
users = users.where(:person => { :address => { :city.matches_any => ['%city1%','%city2%'] } })
Try:
users = users.where(:person => { :address => { :city.matches_any => ['%city1%','%city2%'] } })