diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index bcf7b29..42a4247 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -670,6 +670,20 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal [comments(:greetings)], post.comments_with_interpolated_conditions end + def test_preload_has_one_with_conditions + # pre-heat our cache + Post.arel_table.columns + Comment.columns + + Post.connection.column_calls_by_table['comments'] = 0 + Post.includes(:very_special_comment).all.to_a + assert_equal 0, Post.connection.column_calls_by_table['comments'] + + Post.connection.column_calls_by_table['comments'] = 0 + Post.includes(:first_post_comment).all.to_a + assert_equal 1, Post.connection.column_calls_by_table['comments'] + end + def test_polymorphic_type_condition post = Post.find(posts(:thinking).id, :include => :taggings) assert post.taggings.include?(taggings(:thinking_general)) diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 226600a..1b12f81 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -39,11 +39,14 @@ ActiveRecord::Base.connection.class.class_eval do end ActiveRecord::Base.connection.class.class_eval { - attr_accessor :column_calls + attr_accessor :column_calls, :column_calls_by_table def columns_with_calls(*args) @column_calls ||= 0 + @column_calls_by_table ||= Hash.new {|h,table| h[table] = 0} + @column_calls += 1 + @column_calls_by_table[args.first.to_s] += 1 columns_without_calls(*args) end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 8fe7167..8b50234 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -36,7 +36,7 @@ class Post < ActiveRecord::Base find(:first, :order => "id DESC") end end - + has_many :author_favorites, :through => :author has_many :author_categorizations, :through => :author, :source => :categorizations @@ -44,7 +44,9 @@ class Post < ActiveRecord::Base :conditions => proc { ["#{"#{aliased_table_name}." rescue ""}body = ?", 'Thank you for the welcome'] } has_many :comments_with_deprecated_interpolated_conditions, :class_name => 'Comment', :conditions => ['#{"#{aliased_table_name}." rescue ""}body = ?', 'Thank you for the welcome'] + has_one :first_post_comment, :class_name => 'Comment', :conditions => {:body => 'First Post!'} + has_one :very_special_comment has_one :very_special_comment_with_post, :class_name => "VerySpecialComment", :include => :post has_many :special_comments