diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 461237c7add14f7181419470cc3ddc88c8c021e5..849f01a047fc2866beb00d36a5ba37ea5fcdc60e 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -99,32 +99,32 @@ def create!(*args, &block) # Expects arguments in the same format as Base.create. # # ==== Examples - # # Find the first user named Scarlett or create a new one. + # # Find the first user named Penélope or create a new one. + # User.where(:first_name => 'Penélope').first_or_create + # # => + # + # # Find the first user named Penélope or create a new one. + # # We already have one so the existing record will be returned. + # User.where(:first_name => 'Penélope').first_or_create + # # => + # + # # Find the first user named Scarlett or create a new one with a particular last name. # User.where(:first_name => 'Scarlett').first_or_create(:last_name => 'Johansson') - # # => + # # => # - # # Find the first user named Scarlett or create one with a different last name. - # # We already have one Scarlett, so she'll be returned. + # # Find the first user named Scarlett or create a new one with a different last name. + # # We already have one so the existing record will be returned. # User.where(:first_name => 'Scarlett').first_or_create do |user| # user.last_name = "O'Hara" # end - # # => - # - # # Find the first user named Andy or create several at a time. - # User.where(:first_name => 'Andy').first_or_create([{:last_name => 'García'}, {:last_name => 'Mejía'}]) - # # => [#, - # #] - # - # # Find the first user with last name García or create several at a time. - # User.where(:last_name => 'García').first_or_create([{:first_name => 'Jorge'}, {:first_name => 'Andy'}]) - # # => + # # => def first_or_create(*args, &block) first || create(*args, &block) end # Like first_or_create but calls create! so an exception is raised if the created record is invalid. # - # Expects arguments in the same format as Base.create. + # Expects arguments in the same format as Base.create!. def first_or_create!(*args, &block) first || create!(*args, &block) end