aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-06-16 05:35:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-06-16 05:35:10 +0000
commitbfe6a759c25ad02b4bfcb2dd16999d8ba72e2df8 (patch)
tree0d3274d426dc294ec0deb6109d547162391738c1 /activerecord/lib/active_record/associations/association_collection.rb
parent7b47f150fd73dc84652498a960fc17b601e77a0a (diff)
downloadrails-bfe6a759c25ad02b4bfcb2dd16999d8ba72e2df8.tar.gz
rails-bfe6a759c25ad02b4bfcb2dd16999d8ba72e2df8.tar.bz2
rails-bfe6a759c25ad02b4bfcb2dd16999d8ba72e2df8.zip
Added actual database-changing behavior to collection assigment for has_many and has_and_belongs_to_many #1425 [Sebastian Kanthak]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1428 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb16
1 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 1eac8a2a0b..4495d92151 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -1,3 +1,5 @@
+require 'set'
+
module ActiveRecord
module Associations
class AssociationCollection < AssociationProxy #:nodoc:
@@ -83,11 +85,19 @@ module ActiveRecord
collection.inject([]) { |uniq_records, record| uniq_records << record unless uniq_records.include?(record); uniq_records }
end
+ # Replace this collection with +other_array+
+ # This will perform a diff and delete/add only records that have changed.
def replace(other_array)
- other_array.each{ |val| raise_on_type_mismatch(val) }
+ other_array.each { |val| raise_on_type_mismatch(val) }
+
+ load_target
+ other = other_array.size < 100 ? other_array : other_array.to_set
+ current = @target.size < 100 ? @target : @target.to_set
- @target = other_array
- @loaded = true
+ @owner.transaction do
+ delete(@target.select { |v| !other.include?(v) })
+ concat(other_array.select { |v| !current.include?(v) })
+ end
end
private