Skip to content

Commit

Permalink
providing arel with column information when possible [#5392 state:res…
Browse files Browse the repository at this point in the history
…olved]
  • Loading branch information
tenderlove committed Sep 21, 2010
1 parent f4abe66 commit 96bd936
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions activerecord/lib/active_record/associations.rb
Expand Up @@ -2107,8 +2107,13 @@ def with_join_class(join_class)
def association_join
return @join if @join

aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name, :engine => arel_engine)
parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name, :engine => arel_engine)
aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
:engine => arel_engine,
:columns => klass.columns)

parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
:engine => arel_engine,
:columns => parent.active_record.columns)

@join = case reflection.macro
when :has_and_belongs_to_many
Expand Down Expand Up @@ -2191,7 +2196,9 @@ def association_join
end

def relation
aliased = Arel::Table.new(table_name, :as => @aliased_table_name, :engine => arel_engine)
aliased = Arel::Table.new(table_name, :as => @aliased_table_name,
:engine => arel_engine,
:columns => klass.columns)

if reflection.macro == :has_and_belongs_to_many
[Arel::Table.new(options[:join_table], :as => aliased_join_table_name, :engine => arel_engine), aliased]
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -44,6 +44,16 @@ def test_has_many_uniq_through_count
assert !authors(:mary).unique_categorized_posts.loaded?
end

def test_column_caching
# pre-heat our cache
Post.arel_table.columns
Comment.columns

Post.connection.column_calls = 0
2.times { Post.joins(:comments).to_a }
assert_equal 0, Post.connection.column_calls
end

def test_has_many_uniq_through_find
assert_equal 1, authors(:mary).unique_categorized_posts.find(:all).size
end
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/helper.rb
Expand Up @@ -57,6 +57,15 @@ def execute_with_query_record(sql, name = nil, &block)
alias_method_chain :execute, :query_record
end

ActiveRecord::Base.connection.extend Module.new {
attr_accessor :column_calls
def columns(*args)
@column_calls += 1
super
end
}
ActiveRecord::Base.connection.column_calls = 0

unless ENV['FIXTURE_DEBUG']
module ActiveRecord::TestFixtures::ClassMethods
def try_to_load_dependency_with_silence(*args)
Expand Down

0 comments on commit 96bd936

Please sign in to comment.