From 5edbeb0a87f89f1a51c71afbf248e7fa57891b3c Mon Sep 17 00:00:00 2001 From: Oscar Del Ben Date: Mon, 16 Jul 2012 23:17:22 -0700 Subject: Add docs for Relation initialize, create and create! --- activerecord/lib/active_record/relation.rb | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 05bf3d954c..999a38011c 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -75,6 +75,18 @@ module ActiveRecord binds) end + # Initializes new record from relation while maintaing the current + # scope. + # + # Expects arguments in the same format as +Base.new+. + # + # users = User.where(name: 'DHH') + # user = users.new # => # + # + # You can also pass a block to new with the new record as argument: + # + # user = users.new { |user| user.name = 'Oscar' } + # user.name # => Oscar def new(*args, &block) scoping { @klass.new(*args, &block) } end @@ -87,17 +99,38 @@ module ActiveRecord alias build new + # Tries to +create+ a new record with the same scoped attributes + # defined in the relation. Returns the initialized object if validation fails. + # + # Expects arguments in the same format as +Base.create+. + # + # ==== Examples + # users = User.where(name: 'Oscar') + # users.create # # + # + # users.create(name: 'fxn') + # users.create # # + # + # users.create { |user| user.name = 'tenderlove' } + # # # + # + # users.create(name: nil) # validation on name + # # # def create(*args, &block) scoping { @klass.create(*args, &block) } end + # Similar to #create, but calls +create!+ on the base class. Raises + # an exception if a validation error occurs. + # + # Expects arguments in the same format as Base.create!. def create!(*args, &block) scoping { @klass.create!(*args, &block) } end # Tries to load the first record; if it fails, then create is called with the same arguments as this method. # - # Expects arguments in the same format as Base.create. + # Expects arguments in the same format as +Base.create+. # # ==== Examples # # Find the first user named Penélope or create a new one. -- cgit v1.2.3