Explains the methods required when implementing the search function in rails.
One of the ActiveRecord methods available to the model. With this method, you can retrieve the data that matches the conditions described in the argument.
model.where('Conditional expression including the column to be searched')
The LIKE clause is used when searching for ambiguous strings and is used in conjunction with the where method.
Description when retrieving a name containing c
where('name LIKE(?)', "%c%")
% ・ ・ ・ Arbitrary character string
Now, let's consider the scene where you actually write the code. For example, what kind of code would be used to retrieve data containing the characters Taro in the name column of the tweet table? If you have time, think for yourself.
The actual description is as follows.
Tweet.where('name LIKE(?)', "%Taro%")
With the above description, we were able to create a method that can perform a search. Think about the columns and what values you want to retrieve and try them out!
Thank you for reading to the end! I hope it helps those who have the same worries and challenges!
Recommended Posts