aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-05 16:25:48 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-05 16:25:48 +0000
commit15d88885eedbac1193361a9eea957a7f49e39c9e (patch)
tree9e035506df3e50f4bbffb10c29f33f73d47097ba /activerecord/lib/active_record
parent77730f7c9dc2e28e7f3c84ce647b84eb470dd819 (diff)
downloadrails-15d88885eedbac1193361a9eea957a7f49e39c9e.tar.gz
rails-15d88885eedbac1193361a9eea957a7f49e39c9e.tar.bz2
rails-15d88885eedbac1193361a9eea957a7f49e39c9e.zip
Ensure HABTM#create and HABTM#build do not load entire association. [Pratik]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9229 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb19
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb19
3 files changed, 19 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 141fdcab3d..ce1c8a262d 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -166,6 +166,25 @@ module ActiveRecord
end
protected
+ def load_target
+ if !@owner.new_record? || foreign_key_present
+ begin
+ if !loaded?
+ if @target.is_a?(Array) && @target.any?
+ @target = find_target + @target.find_all {|t| t.new_record? }
+ else
+ @target = find_target
+ end
+ end
+ rescue ActiveRecord::RecordNotFound
+ reset
+ end
+ end
+
+ loaded if target
+ target
+ end
+
def method_missing(method, *args)
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
if block_given?
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index b06b618d5a..0edb2397ee 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -7,7 +7,6 @@ module ActiveRecord
end
def build(attributes = {})
- load_target
build_record(attributes)
end
@@ -154,7 +153,6 @@ module ActiveRecord
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr) }
else
- load_target
build_record(attributes, &block)
end
end
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index eedffa052b..de6b843098 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -68,25 +68,6 @@ module ActiveRecord
end
protected
- def load_target
- if !@owner.new_record? || foreign_key_present
- begin
- if !loaded?
- if @target.is_a?(Array) && @target.any?
- @target = (find_target + @target).uniq
- 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)