replace(other_array)
Instance Public methods
Replaces this collection with other_array
. This will perform a
diff and delete/add only records that have changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Person < ActiveRecord::Base has_many :pets end person.pets # => [#<Pet id: 1, name: "Gorby", group: "cats", person_id: 1>] other_pets = [Pet. new (name: 'Puff' , group: 'celebrities' ] person.pets.replace(other_pets) person.pets # => [#<Pet id: 2, name: "Puff", group: "celebrities", person_id: 1>] |
If the supplied array has an incorrect association type, it raises an
ActiveRecord::AssociationTypeMismatch
error:
1 2 | person.pets.replace([ "doo" , "ggie" , "gaga" ]) # => ActiveRecord::AssociationTypeMismatch: Pet expected, got String |
Please login to continue.