From 15d88885eedbac1193361a9eea957a7f49e39c9e Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 5 Apr 2008 16:25:48 +0000 Subject: 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 --- .../associations/association_collection.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'activerecord/lib/active_record/associations/association_collection.rb') 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? -- cgit v1.2.3