diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-05-21 19:17:37 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-05-21 19:17:37 +0000 |
commit | 66586bc168f358e3385416def6eb30ad1242ffe6 (patch) | |
tree | 5123f76617348e2f3ad908945586f426650b097b /activerecord/lib/active_record/associations | |
parent | 59c8c63ecd751136c5ed6d2e3c04a54af2025eb0 (diff) | |
download | rails-66586bc168f358e3385416def6eb30ad1242ffe6.tar.gz rails-66586bc168f358e3385416def6eb30ad1242ffe6.tar.bz2 rails-66586bc168f358e3385416def6eb30ad1242ffe6.zip |
Fixed that has_many collections shouldn't load the entire association to do build or create [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4354 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 29 |
2 files changed, 35 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 428152eea0..9c27411a3e 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -95,7 +95,14 @@ 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] then @target.size else count_records end + if loaded? && !@reflection.options[:uniq] + @target.size + elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) + unsaved_records = Array(@target.detect { |r| r.new_record? }).size + unsaved_records + count_records + else + count_records + end end # Returns the size of the collection by loading it and calling size on the array. If you want to use this method to check diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 912e0aa390..6a2089c7b8 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -10,10 +10,16 @@ module ActiveRecord if attributes.is_a?(Array) attributes.collect { |attr| build(attr) } else - load_target record = @reflection.klass.new(attributes) set_belongs_to_association_for(record) - @target << record + + if loaded? + @target << record + else + @target ||= [] + @target << record + end + record end end @@ -107,6 +113,25 @@ module ActiveRecord end end + def load_target + if !@owner.new_record? || foreign_key_present + begin + if !loaded? + if @target.is_a?(Array) && @target.any? + @target = find_target + @target + else + @target = find_target + end + end + rescue ActiveRecord::RecordNotFound + reset + end + end + + loaded if target + target + end + def count_records count = if has_cached_counter? @owner.send(:read_attribute, cached_counter_attribute_name) |