aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb18
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb12
-rw-r--r--activerecord/test/associations_join_model_test.rb27
-rw-r--r--activerecord/test/fixtures/post.rb2
-rw-r--r--activerecord/test/fixtures/tagging.rb2
6 files changed, 49 insertions, 14 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 03d71ee578..e92cf57e4f 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Quit ignoring default :include options in has_many :through calls [Mark James]
+
* Allow has_many :through associations to find the source association by setting a custom class (closes #4307) [jonathan@bluewire.net.nz]
* Eager Loading support added for has_many :through => :has_many associations (see below). [Rick Olson]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 2ed9e27e7f..14a3eeb1eb 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -832,12 +832,12 @@ module ActiveRecord
end
def count_with_associations(options = {})
- join_dependency = JoinDependency.new(self, options[:include])
+ join_dependency = JoinDependency.new(self, options[:include], options[:joins])
return count_by_sql(construct_counter_sql_with_included_associations(options, join_dependency))
end
def find_with_associations(options = {})
- join_dependency = JoinDependency.new(self, options[:include])
+ join_dependency = JoinDependency.new(self, options[:include], options[:joins])
rows = select_all_rows(options, join_dependency)
return join_dependency.instantiate(rows)
end
@@ -1101,8 +1101,8 @@ module ActiveRecord
class JoinDependency
attr_reader :joins, :reflections, :table_aliases
- def initialize(base, associations)
- @joins = [JoinBase.new(base)]
+ def initialize(base, associations, joins)
+ @joins = [JoinBase.new(base, joins)]
@associations = associations
@reflections = []
@base_records_hash = {}
@@ -1199,12 +1199,13 @@ module ActiveRecord
end
class JoinBase
- attr_reader :active_record
+ attr_reader :active_record, :table_joins
delegate :table_name, :column_names, :primary_key, :reflections, :sanitize_sql, :to => :active_record
- def initialize(active_record)
+ def initialize(active_record, joins = nil)
@active_record = active_record
@cached_record = {}
+ @table_joins = joins
end
def aliased_prefix
@@ -1258,6 +1259,11 @@ module ActiveRecord
@aliased_prefix = "t#{ join_dependency.joins.size }"
@aliased_table_name = sti? ? pluralize(reflection.name) : table_name # start with the table name
@parent_table_name = sti? ? pluralize(parent.active_record.name.underscore) : parent.active_record.table_name
+
+ if !parent.table_joins.blank? && parent.table_joins =~ %r{#{aliased_table_name}}
+ join_dependency.table_aliases[aliased_table_name] += 1
+ end
+
unless join_dependency.table_aliases[aliased_table_name].zero?
# if the table name has been used, then use an alias
@aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}"
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 1baac52cdd..4e0b6f1c98 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -24,9 +24,10 @@ module ActiveRecord
options[:order] = @reflection.options[:order]
end
- options[:select] = construct_select
- options[:from] = construct_from
- options[:joins] = construct_joins
+ options[:select] = construct_select
+ options[:from] = construct_from
+ options[:joins] = construct_joins
+ options[:include] ||= @reflection.source_reflection.options[:include]
merge_options_from_reflection!(options)
@@ -57,7 +58,8 @@ module ActiveRecord
:joins => construct_joins,
:order => @reflection.options[:order],
:limit => @reflection.options[:limit],
- :group => @reflection.options[:group]
+ :group => @reflection.options[:group],
+ :include => @reflection.options[:include] || @reflection.source_reflection.options[:include]
)
end
@@ -96,7 +98,7 @@ module ActiveRecord
end
"INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]}" % [
- @owner.class.reflections[@reflection.options[:through]].table_name,
+ @reflection.through_reflection.table_name,
@reflection.table_name, reflection_primary_key,
@reflection.through_reflection.table_name, source_primary_key
]
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb
index 4ea2b6e684..d7a230ddba 100644
--- a/activerecord/test/associations_join_model_test.rb
+++ b/activerecord/test/associations_join_model_test.rb
@@ -36,9 +36,34 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
end
def test_polymorphic_has_many_going_through_join_model
- assert_equal tags(:general), posts(:welcome).tags.first
+ assert_equal tags(:general), tag = posts(:welcome).tags.first
+ assert_no_queries do
+ tag.tagging
+ end
+ end
+
+ def test_polymorphic_has_many_going_through_join_model_with_find
+ assert_equal tags(:general), tag = posts(:welcome).tags.find(:first)
+ assert_no_queries do
+ tag.tagging
+ end
end
+ def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection
+ assert_equal tags(:general), tag = posts(:welcome).funky_tags.first
+ assert_no_queries do
+ tag.tagging
+ end
+ end
+
+ def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection_with_find
+ assert_equal tags(:general), tag = posts(:welcome).funky_tags.find(:first)
+ assert_no_queries do
+ tag.tagging
+ end
+ end
+
+
def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key
assert_equal tags(:misc), taggings(:welcome_general).super_tag
assert_equal tags(:misc), posts(:welcome).super_tags.first
diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb
index eca9369723..d3fa126b88 100644
--- a/activerecord/test/fixtures/post.rb
+++ b/activerecord/test/fixtures/post.rb
@@ -21,7 +21,7 @@ class Post < ActiveRecord::Base
has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
has_many :taggings, :as => :taggable
- has_many :tags, :through => :taggings
+ has_many :tags, :through => :taggings, :include => :tagging
has_many :funky_tags, :through => :taggings, :class_name => 'Tag'
has_many :super_tags, :through => :taggings
has_one :tagging, :as => :taggable
diff --git a/activerecord/test/fixtures/tagging.rb b/activerecord/test/fixtures/tagging.rb
index 51c3bd3604..4695f0755e 100644
--- a/activerecord/test/fixtures/tagging.rb
+++ b/activerecord/test/fixtures/tagging.rb
@@ -1,5 +1,5 @@
class Tagging < ActiveRecord::Base
- belongs_to :tag
+ belongs_to :tag, :include => :tagging
belongs_to :super_tag, :class_name => 'Tag', :foreign_key => 'super_tag_id'
belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
belongs_to :taggable, :polymorphic => true, :counter_cache => true