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.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
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 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 address automatic input implementation method
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
Ruby on Rails Refactoring method example summary around MVC
(Ruby on Rails6) How to create models and tables
[Ruby on Rails] If there are duplicate records when registering data, do not register
Portfolio creation Ruby on Rails
[Ruby on Rails] When parameter id acquisition does not work
[Ruby on Rails] Debug (binding.pry)
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
Cancel Ruby on Rails migration
[Ruby] Notes on gets method
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Read try (: [],: key)
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[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 on Rails] Stop "looping until ..."
How to create a query using variables in GraphQL [Using Ruby on Rails]
[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] Follow function undefined method ʻid'for nil: NilClass error resolution
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] model, controller terminal command