Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Corrected documentation of has_many methods collection.build and coll…

…ection.create.

* collection.create: Clarified the wording (this had always confused me). I used the phrase
"base model" to mean the model that specifies the has_many association (this wording is also
used previously, e.g. in the "Cardinality and associations" section).

* collection.build: You *can* actually call this to add new association records when the base
model is a new record. In ActiveRecord::Associations::AssociationCollection, #create_record
calls #ensure_owner_is_not_new, but #build_record does not.
  • Loading branch information...
commit 44e8f7cf00d94ddcd9b02471782c76cc6e288739 1 parent 2ad0618
Dave Rothlisberger authored
Showing with 3 additions and 4 deletions.
  1. +3 −4 activerecord/lib/active_record/associations.rb
View
7 activerecord/lib/active_record/associations.rb
@@ -767,13 +767,12 @@ module ClassMethods
# [collection.build(attributes = {}, ...)]
# Returns one or more new objects of the collection type that have been instantiated
# with +attributes+ and linked to this object through a foreign key, but have not yet
- # been saved. <b>Note:</b> This only works if an associated object already exists, not if
- # it's +nil+!
+ # been saved.
# [collection.create(attributes = {})]
# Returns a new object of the collection type that has been instantiated
# with +attributes+, linked to this object through a foreign key, and that has already
- # been saved (if it passed the validation). <b>Note:</b> This only works if an associated
- # object already exists, not if it's +nil+!
+ # been saved (if it passed the validation). *Note*: This only works if the base model
+ # already exists, not if it's +nil+ or is a new (unsaved) record!
#
# (*Note*: +collection+ is replaced with the symbol passed as the first argument, so
# <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.)
Please sign in to comment.
Something went wrong with that request. Please try again.