diff options
author | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-17 12:40:35 -0500 |
---|---|---|
committer | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-17 12:40:35 -0500 |
commit | cd840c3e3838c2396176ecfc2820c701080ec115 (patch) | |
tree | 40d4adc3110af56f74a4915e4363e70a62201618 /activerecord/lib | |
parent | 03f8a574872f1206c16720af034e4bb91930d237 (diff) | |
download | rails-cd840c3e3838c2396176ecfc2820c701080ec115.tar.gz rails-cd840c3e3838c2396176ecfc2820c701080ec115.tar.bz2 rails-cd840c3e3838c2396176ecfc2820c701080ec115.zip |
Add docs to CollectionAssociation#replace
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 26 |
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 |