fuzzy search with active record query interface

三世轮回 提交于 2019-12-08 02:32:23

问题


I have a fuzzy search in my rails app, which sql what I want is this:

select * from `user` where name like '%abc%'

I've tried to do it like this:

name = 'abc'
User.where("name like '%?%'", name)

It failed, in console it logged:

select * from `user` where name like '%'abc'%'

Finally I tried this

name = 'abc'
User.where("name like ?", '%' + name + '%')

It worked.

But I think it doesn't like rails way, is there any better way to do that?


回答1:


User.where("name REGEXP ?", 'regex_str')

and regex_str should be MySQL regex string

Try this..



来源:https://stackoverflow.com/questions/25971869/fuzzy-search-with-active-record-query-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!