问题
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