Quit ignoring default :include options in has_many :through calls [Mark James]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3974 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Rick Olson
2006-03-19 16:13:52 +00:00
parent fcd4c9529c
commit ea25e246e0
6 changed files with 49 additions and 14 deletions

View File

@@ -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]

View File

@@ -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}"

View File

@@ -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
]

View File

@@ -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

View File

@@ -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

View File

@@ -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