Q:

Rails how do I search in the first and second part of a string in the database?

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'

A:

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)

