aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-10-16 15:42:55 +0000
committerRick Olson <technoweenie@gmail.com>2006-10-16 15:42:55 +0000
commitc5536f9f00c7ead30de8d3d2f12d6d8fe2b79a3c (patch)
treed8991ffb46d5b71e8e5a38006d08769171a4d95f /activerecord
parentb5ec0fe3133531f0cce5b8707f2ac831fdc57bea (diff)
downloadrails-c5536f9f00c7ead30de8d3d2f12d6d8fe2b79a3c.tar.gz
rails-c5536f9f00c7ead30de8d3d2f12d6d8fe2b79a3c.tar.bz2
rails-c5536f9f00c7ead30de8d3d2f12d6d8fe2b79a3c.zip
Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney] (sorry, forgot to commit the actual files)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5310 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/associations.rb20
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb5
3 files changed, 20 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index cef8bccba1..babc15361b 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix has_many :through to add the appropriate conditions when going through an association using STI. Closes #5783. [Jonathan Viney]
+
* fix select_limited_ids_list issues in postgresql, retain current behavior in other adapters [Rick]
* Restore eager condition interpolation, document it's differences [Rick]
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 505ebea9f6..f40e44e804 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1514,15 +1514,21 @@ module ActiveRecord
when :belongs_to
first_key = primary_key
second_key = source_reflection.options[:foreign_key] || klass.to_s.classify.foreign_key
+ extra = nil
when :has_many
- first_key = through_reflection.klass.to_s.classify.foreign_key
+ first_key = through_reflection.klass.base_class.to_s.classify.foreign_key
second_key = options[:foreign_key] || primary_key
+ extra = through_reflection.klass.descends_from_active_record? ? nil :
+ " AND %s.%s = %s" % [
+ aliased_join_table_name,
+ reflection.active_record.connection.quote_column_name(through_reflection.active_record.inheritance_column),
+ through_reflection.klass.quote_value(through_reflection.klass.name.demodulize)]
end
- " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
+ " LEFT OUTER JOIN %s ON (%s.%s = %s.%s%s) " % [
table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
aliased_join_table_name, through_reflection.primary_key_name,
- parent.aliased_table_name, parent.primary_key] +
- " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
+ parent.aliased_table_name, parent.primary_key, extra] +
+ " LEFT OUTER JOIN %s ON (%s.%s = %s.%s) " % [
table_name_and_alias,
aliased_table_name, first_key,
aliased_join_table_name, second_key
@@ -1566,7 +1572,11 @@ module ActiveRecord
aliased_table_name,
reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column),
klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record?
- join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
+
+ [through_reflection, reflection].each do |ref|
+ join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions]))} " if ref && ref.options[:conditions]
+ end
+
join
end
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 cbc5616aa8..ce22496e6d 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -226,8 +226,9 @@ module ActiveRecord
def conditions
@conditions ||= [
(interpolate_sql(@reflection.klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]),
- (interpolate_sql(@reflection.through_reflection.klass.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions])
- ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions])
+ (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]),
+ ("#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" unless @reflection.through_reflection.klass.descends_from_active_record?)
+ ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions] && @reflection.through_reflection.klass.descends_from_active_record?)
end
alias_method :sql_conditions, :conditions