From 1b63ddd5e356344f525b90213e6f8efb807fddbc Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Tue, 7 Oct 2008 13:49:57 +1300 Subject: [PATCH] additional #1110 test & fix for HABTM - requires my main #1110 patch + Fred's #1060 patch to fix HABTM first --- .../lib/active_record/association_preload.rb | 1 + activerecord/test/cases/associations/eager_test.rb | 10 ++++++++++ activerecord/test/models/post.rb | 1 + 3 files changed, 12 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 6e194ab..1fee6c8 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -175,6 +175,7 @@ module ActiveRecord end def preload_has_and_belongs_to_many_association(records, reflection, preload_options={}) + return if records.first.send(reflection.name).loaded? table_name = reflection.klass.quoted_table_name id_to_record_map, ids = construct_id_map(records) records.each {|record| record.send(reflection.name).loaded} diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 1d801cb..48f2ef4 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -151,6 +151,16 @@ class EagerAssociationTest < ActiveRecord::TestCase end end + def test_finding_with_includes_on_habtm_association_with_same_include_includes_only_once + post = Post.find(posts(:welcome).id, :include => {:categories_with_posts => :posts}) + assert !post.categories_with_posts.empty? + post.categories_with_posts.each do |categories_with_posts| + assert !categories_with_posts.posts.empty? + assert_equal categories_with_posts.posts.length, categories_with_posts.posts.count + assert_equal nil, categories_with_posts.posts.uniq! + end + end + def test_loading_from_an_association posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id") assert_equal 2, posts.first.comments.size diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index e0d8be6..bfc4dc5 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -34,6 +34,7 @@ class Post < ActiveRecord::Base has_many :nonexistant_comments, :class_name => 'Comment', :conditions => 'comments.id < 0' has_and_belongs_to_many :categories + has_and_belongs_to_many :categories_with_posts, :class_name => "Category", :association_foreign_key => "category_id", :include => :posts has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id' has_many :taggings, :as => :taggable -- 1.5.5.1