[Rails] Difference between find and find_by

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6. There was some ambiguity about find and find_by, so I tried to summarize it myself.

find --Specify __id (primary key) __ as an argument. --The image is to get the record with the specified id. --If the id is not found, a __exception (RecordNotFound) will be returned. __ You get a red error page, right? --It is possible to specify multiple arguments.

Model name.find(id data)

――Specifically, use it as follows.

Item.find(1) # =>Returns a record with id 1
Item.find(1,3,5) # =>Returns the specified id as an array

find_by --Data can be acquired only for the first match __. --You can also use column names other than id as search conditions. --Returns nil if none match the conditions. It is convenient when you want to continue processing after the description of find_by even if there is no acquired data. --Multiple conditions can be specified.

Model name.find_by(conditions)

――Specifically, use it as follows.

Article.find_by(title: 'hoge') 
# =>title is'hoge'Returns the first matching data in

Fruit.find_by(name: 'apple', color: 'red') 
# =>name is'apple'And the color is'red'Returns the data of

When two conditions are specified and only one does not match

Fruit.find_by(name: 'apple', color: 'blue') #=>Returns nil

Recommended Posts

[Rails] Difference between find and find_by
[rails] Difference between redirect_to and render
[Rails] Difference between redirect_to and render [Beginner]
Difference between vh and%
Difference between i ++ and ++ i
[Rails / ActiveRecord] About the difference between create and create!
Difference between member and collection of rails routes.rb
[Rails] Difference between create method and new + save method
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Rails] I learned about the difference between resources and resources
[Rails] What is the difference between redirect and render?
[Rails] I investigated the difference between redirect_to and render.
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Find the difference between List types
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Rails] What is the difference between bundle install and bundle update?
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Difference between Java and JavaScript (how to find the average)
Rough difference between RSpec and minitest
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
Differences between find, find_by, find_by_sql methods
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Android] Difference between finish (); and return;
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry