-1

For my application I want to build a search form that gives back the name of the streets. But when I use where('street like ?', 'wall') it only returns: 'Wallstreet' and not for example: 'Second Wallstreet, The Wallstreet'

I want to return also the 'Second Wallstreet, The Wallstreet' but not the a street name with 'Notwallstreet', where 'Wall' is somewhere in the middle.

I have used the 'gem ransack' but there I only found the option contains. And that wil also return 'Notwallstreet'

Bart Hoekstra
  • 5,814
  • 1
  • 15
  • 13

2 Answers2

1

If you're using postgres you can use SIMILAR TO as described here.

where('street similar to ?', '[[:<:]]Wall')
davidrac
  • 10,723
  • 3
  • 39
  • 71
1

You need to put % on both sides of your word to search for a prefix/infix/postfix

where('street like ?', '%wall%')

PS:

As far as you don't need infix:

where('street like ? or ?', '%wall', 'wall%')

And here is the thing you should be careful. If you have got an index on the street field postfix match won't use it (and prefix will)

fl00r
  • 82,987
  • 33
  • 217
  • 237