Ruby on Rails record search, create if not find_or_create_by method

What is the find_or_create_by method?

The find_or_create_by method is a combination of the find method and the create method. Search the table for records that meet the conditions passed in the argument A method that gets the corresponding record if it exists, and creates it if it doesn't.

How to use

** Model .find_or_create_by (search condition) **

Example: To get a record with name "suzuki" from the users table, or create it if it doesn't exist

User.find_or_create_by(name: "suzuki")

Recommended Posts

Ruby on Rails record search, create if not find_or_create_by method
[Ruby on Rails] Search function (not selected)
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
[Ruby on Rails] Search function (model, method selection formula)
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Use the resources method to automatically create routes.
Ruby on Rails installation method [Mac edition]
Ruby on Rails controller create / delete command
[Ruby on Rails] About Active Record callbacks
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
[Ruby on Rails] Search function (not selected)
Ruby on Rails address automatic input implementation method
[Ruby on Rails] How to use session method
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Introduction] Try to create a Ruby on Rails application
Ruby on Rails Refactoring method example summary around MVC
(Ruby on Rails6) How to create models and tables
Portfolio creation Ruby on Rails
[Ruby on Rails] Debug (binding.pry)
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Commentary on partial! --Ruby on Rails
Cancel Ruby on Rails migration
[Ruby] Notes on gets method
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
(Ruby on Rails6) Create a function to edit the posted content
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
[Ruby] present/blank method and postfix if.
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby] Search problem using index method
[Ruby on Rails] Stop "looping until ..."
How to create a query using variables in GraphQL [Using Ruby on Rails]
[Ruby on Rails] Create a pie chart for each column with Chartkick
[Ruby on Rails] NoMethodError: undefined method `t'for # <ActiveRecord :: Migration: 〇〇〇〇> Dealing with errors
What to do if deployment fails on Heroku (Ruby app not detected)
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
[Ruby On Rails] How to search the contents of params using include?