[RUBY] about the where method (rails)

What is the where method?

A method that can retrieve records that match the conditions in the table in the form of an array.


Model name.where("conditions")

Basic description method

The where method has a symbol specification and a character string specification.

① Symbol designation


Model name.where(Column name:conditions)

#When retrieving all records containing the characters "Taro" from the name column of the users table
User.where(name: "Taro")

① Character string specification


Model name.where("Column name=conditions")

#When retrieving all records containing the characters "Taro" from the name column of the users table
User.where("name = 'Taro'")

Various search methods and complicated specification methods

① How to compare and define conditions


User.where("id > ?", 5)
#You can get all records with id 5 or higher.

(2) AND search method with multiple columns specified


User.where(name: "Taro", age: 25)
#You can get all records with "Taro" in the name column and "25" in the age column.

③ OR search method by specifying multiple columns


User.where(name: "Taro").or(User.where(age: 25))
#You can get all records with name column "Taro" or age column "25".

Recommended Posts

about the where method (rails)
About the method
Output about the method # 2
About the length method
About the authenticate method.
About the map method
About the ancestors method
About the to_s method.
Output about the method Part 1
Consideration about the times method
About Rails scraping method Mechanize
About Rails 6
rails method
[Rails] About the Punk List function
About the symbol <%%> in Rails erb
About the role of the initialize method
[Rails] About helper method form_with [Basic]
[Rails 6] destroy using the resources method
[rails] The case where the server stopped working
About Rails routing
[Rails] How to use the map method
Rails delegate method
Where about java
[Rails] Talk about paying attention to the return value of where
[Rails] About ActiveJob ,!
About the package
About Rails controller
About RSpec (Rails)
[Order method] Set the order of data in Rails
[Rails / ActiveRecord] About the difference between create and create!
A note about the Rails and Vue process
About No Method Error
[Rails] About migration files
About the StringBuilder class
[Rails 6] About main gems
Commentary: About the interface
[Rails] About active hash
About rails application server
About the asset pipeline
[Rails] devise helper method
About the function double-java
About rails kaminari pagination
About rails version specification
About Java method binding
About the ternary operator
About method splitting (Java)
MEMO about Rails 6 series
About the Kernel module
[Rails] About Slim notation
[Rails] devise introduction method
[Output] About the database
About the case where "Docker" freeter tried to put Docker in the existing Rails application
[rails] About devise defaults
About the [ruby] operator
Master the [Rails] scope!
Rails: About partial templates
About rails strong parameters
[Beginner] About Rails Session
[Rails] I learned about the difference between resources and resources
Implement post search function in Rails application (where method)
Questions about implementing the Like feature (Ajax) in Rails