[RAILS] Method to search


Explains the methods required when implementing the search function in rails.

1. where method

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')

2. LIKE clause

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

3. Code for searching

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

Method to search
Binary search binary search method
Binary search method
16 Corresponds to method invocation
Practice of binary search method
20 Corresponds to static method invocation
Practice of linear search method
Easy to use array.map (&: method)
How to create a method
to_ ○
How to use the link_to method
Item 43: Prefer method references to lambdas
How to use the include? method
How to use the form_with method
[Java] How to use join method
Introduction to algorithms with java-Shakutori method
Introduction to Design Patterns (Factory Method)
[Ruby] How to use any? Method
I tried to explain the method
Try to extract java public method
[Ruby] Search problem using index method
How to use Ruby inject method
How to implement search functionality in Rails
Java method
to_i method
java (method)
[Java] How to compare with equals method
getRequestDispatcher () method
merge method
How to use submit method (Java Silver)
[Ruby] From the basics to the inject method
[Rails] How to use the map method
Introduction to algorithms with java --Search (depth-first search)
[Java] How to use the toString () method
Map method
I tried to solve AOJ's Binary Search
include method
Abstract method
How to have params in link_to method
initialize method
I wanted to add @VisibleForTesting to the method
List method
[SwiftUI] Implementation method equivalent to changing UIWindow.rootViewController
I was addicted to the roll method
puts method
Linear search
Java method
Class method
Add Document to Azure Search Service (Java)
save! method
getParameter method
Association (1 to 1)! !!
[Java] method
[Rails] Method summary for conversion / verification / search
private method
What to do when undefined method ʻuser_signed_in?'
rails method
[Rails] How to use helper method, confimartion
[Java] method
Introduction to algorithms with java --Search (breadth-first search)