diff options
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 18 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 12 |
2 files changed, 19 insertions, 11 deletions
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 ] |