Rails: A brief summary of find, find_by, where

At first

** Day 23 of Calendar Planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.

About find, find_by, where

It is the content that I personally did not understand and investigated and interpreted in my own way. I think there are many other articles that are detailed, so I wonder if you can think about it as a general outline here ^^; (Even if you write in detail, it will be a copy and paste at some point ^^;)

find, find_by, where These are the understandings necessary to retrieve only the necessary information from the DB (database). If you want to create a web application, use one of them.

I remember this much

(Search is my interpretation. Please note)

Method Search range Data to be acquired
find id One
find_by Specified column First one
where Specified column all

find

I remember this much ↓ ** Get one record found by specifying id (primary key). ** **

If not found, you will get the error ActiveRecord :: RecordNotFound.

#Instance variables=Model name.find (primary key)

@post = Post.find(params[:id])

I will describe it like this.

The part contains 1 or 2 or 3, ,,, n in id and gets the one related to it. If it is 1, => post.find (1) and get the data of id: 1 of post.

find_by

I remember this much ↓ ** You can specify columns other than id with + α for the role of find **

In find, the columns that can be specified (searchable) were limited to id. find_by can be specified (searchable) columns other than id are OK.

The same 1 record found as the search result will be fetched. You can also specify characters, but only the first one you find will be applied.


#Model name.find_by(Arbitrary column name:Stored value)

@post = Post.find_by(title: "Qiita")

Get one that stores "Qiita" in the title column of Post.

Use find to find by id Otherwise use find_by

where

I remember this much ↓ ** There are multiple records to be acquired by + α in the role of find_by. ** **

where can be specified (searchable) in the same way as find_by Columns other than id are OK

Search results are all applicable, not just one.


#Model name.where(Arbitrary column name:Stored value)

@post = Post.where(title: "Qiita")

In find_by, there was one with "Qiita" in the title, Where will get everything that has "Qiita" in the title.

Finally

I'm sorry if the interpretation is wrong. Since I get data from a database, I personally imagined a machine that searches in a library or a book search machine in a manga cafe. Then I felt that I could understand it, so I posted it ^ ^

Recommended Posts

Rails: A brief summary of find, find_by, where
A brief summary of Rails association options (foreign_key, primary_key)
A summary of only Rails tutorial setup related
A brief summary of Bootstrap features for beginners
A brief explanation of commitAllowingStateLoss
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Summary of rails validation (for myself)
[Rails] Summary of complicated routing configurations
[Rails] Difference between find and find_by
A brief description of JAVA dependencies
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Rails Basics of creating a new application
Rails: A little summary about data types
Find the difference from a multiple of 10
Summary of basic knowledge of Rails acquired by progate
[Ruby on Rails] A memorandum of layout templates
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
[Note] Summary of rails login function using devise ①
Rails 6.0 Routing Summary
rails db: 〇〇 Summary
Summary of frequently used commands in Rails and Docker
A review of the code used by rails beginners
One case of solving a migration error in Rails
[Rails] Volume that displays favorites and a list of favorites
A record of a brief touch on GCP's Data Flow
A summary of what Java programmers find when reading Kotlin source for the first time