aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/collection_association.rb
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-17 12:40:35 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-17 12:40:35 -0500
commitcd840c3e3838c2396176ecfc2820c701080ec115 (patch)
tree40d4adc3110af56f74a4915e4363e70a62201618 /activerecord/lib/active_record/associations/collection_association.rb
parent03f8a574872f1206c16720af034e4bb91930d237 (diff)
downloadrails-cd840c3e3838c2396176ecfc2820c701080ec115.tar.gz
rails-cd840c3e3838c2396176ecfc2820c701080ec115.tar.bz2
rails-cd840c3e3838c2396176ecfc2820c701080ec115.zip
Add docs to CollectionAssociation#replace
Diffstat (limited to 'activerecord/lib/active_record/associations/collection_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb26
1 files changed, 23 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 3af5ff3eab..90899b2078 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -15,7 +15,7 @@ module ActiveRecord
#
# If you need to work on all current children, new and existing records,
# +load_target+ and the +loaded+ flag are your friends.
- class CollectionAssociation < Association #:nodoc:
+ class CollectionAssociation < Association
# Implements the reader method, e.g. foo.items for Foo.has_many :items
def reader(force_reload = false)
@@ -298,8 +298,28 @@ module ActiveRecord
end
end
- # Replace this collection with +other_array+
- # This will perform a diff and delete/add only records that have changed.
+ # Replace this collection with +other_array+. This will perform a diff
+ # and delete/add only records that have changed.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # person.pets
+ # # => [ #<Pet name: "Snoop", type: "dog">, #<Pet name: "Wy", type: "cat"> ]
+ #
+ # other_pets = [Pet.new(name: 'GorbyPuff', type: 'celibrity')]
+ #
+ # person.pets.replace(other_pets)
+ #
+ # person.pets
+ # # => [ #<Pet name: "GorbyPuff", type: "celebrity"> ]
+ #
+ # If the supplied array has an incorrect association type, it raises
+ # an ActiveRecord::AssociationTypeMismatch error:
+ #
+ # person.pets.replace(["doo", "ggie", "gaga"])
+ # #=> ActiveRecord::AssociationTypeMismatch: Pet expected, got String
def replace(other_array)
other_array.each { |val| raise_on_type_mismatch(val) }
original_target = load_target.dup