I'm trying to display all Guides where title contains the word 'attack'. I do so like so:
def attack @guides = Guide.where(title: 'attack') end
Quick question - How do I make it non case-sensitive and restrictive? It can contain any spelling of the word 'attack' like 'AtTaCK', and it can have more words. As long as Attack is in it somehow.
Then, to print them out in the view, I do this:
<% @guides.each do |guide| %> <%= guide.title %> <% end %>
This question is related to
ruby-on-rails
ruby
activerecord
view
controller
You can use the lower
function:
Guide.where("lower(title)='attack'")
As a comment: Work on your question. The title isn't terribly informative, and you drop a big chunk of code at the end that is irrelevant to your question.
Stuarts' answer is correct, but if you are not sure if you are saving the titles in lowercase, you can also make a case insensitive search
There are a lot of answered questions in Stack Overflow with more data on this:
Source: Stackoverflow.com