find_or_create snippets

Rails find_or_initialize and find_or_create methods are deprecated

Tagged rails, activerecord, find_or_create, find_or_initialize  Languages ruby

The find_or_initialize and find_or_create methods have been deprecated in Rails x.x.x. This is what you need to do:

Before:
User.find_or_create_by_name('XXX')

After:
User.where(name: 'XXX').first_or_create
User.where(name: 'XXX').first_or_create!

Before:
User.find_or_initialize_by_name('XXX')

After:
User.where(name: 'XXX').first_or_initialize

If you want the old methods, see https://github.com/rails/activerecord-deprecated_finders.

Also see http://apidock.com/rails/ActiveRecord/Relation/first_or_create