From 4cf8bf7312a124cb96e615923ebb373380fb60bd Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 21 Dec 2006 23:28:12 +0000 Subject: Pushing a record on an association collection doesn't unnecessarily load all the associated records. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5769 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_record/associations/association_collection.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index d3b76e2087..bda72637c3 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -7,7 +7,7 @@ module ActiveRecord load_target @target.to_ary end - + def reset reset_target! @loaded = false @@ -17,7 +17,6 @@ module ActiveRecord # Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically. def <<(*records) result = true - load_target @owner.transaction do flatten_deeper(records).each do |record| @@ -34,7 +33,7 @@ module ActiveRecord alias_method :push, :<< alias_method :concat, :<< - + # Remove all records from this association def delete_all load_target @@ -103,7 +102,7 @@ module ActiveRecord # calling collection.size if it has. If it's more likely than not that the collection does have a size larger than zero # and you need to fetch that collection afterwards, it'll take one less SELECT query if you use length. def size - if loaded? && !@reflection.options[:uniq] + if @owner.new_record? || (loaded? && !@reflection.options[:uniq]) @target.size elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) unsaved_records = Array(@target.detect { |r| r.new_record? }) -- cgit v1.2.3