aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-03-18 22:38:49 +0000
committerRick Olson <technoweenie@gmail.com>2006-03-18 22:38:49 +0000
commitfbd1e01bd6ca1b093811b0781d85f53850df12e3 (patch)
treecad9138fbecb159c9820f5aaed2d3d8ffdac0915 /activerecord/lib/active_record/associations/has_many_through_association.rb
parent1e7ce13b372e554438aa58c466dc100ef174ae9e (diff)
downloadrails-fbd1e01bd6ca1b093811b0781d85f53850df12e3.tar.gz
rails-fbd1e01bd6ca1b093811b0781d85f53850df12e3.tar.bz2
rails-fbd1e01bd6ca1b093811b0781d85f53850df12e3.zip
Fix that has_many :through honors the foreign key set by the belongs_to association in the join model (closes #4259) [andylien@gmail.com / Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3954 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb7
1 files changed, 5 insertions, 2 deletions
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 e242b9f7a1..3d3d53c677 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -65,14 +65,17 @@ module ActiveRecord
raise ActiveRecordError, "Could not find the association '#{@reflection.options[:through]}' in model #{@reflection.klass}"
end
+ # Get the actual primary key of the belongs_to association that the reflection is going through
+ source_primary_key = through_reflection.klass.reflect_on_association(@reflection.name.to_s.singularize.to_sym).primary_key_name
+
if through_reflection.options[:as]
conditions =
- "#{@reflection.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " +
+ "#{@reflection.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{source_primary_key} " +
"AND #{through_reflection.table_name}.#{through_reflection.options[:as]}_id = #{@owner.quoted_id} " +
"AND #{through_reflection.table_name}.#{through_reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"
else
conditions =
- "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{@reflection.klass.to_s.foreign_key} " +
+ "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key} = #{through_reflection.table_name}.#{source_primary_key} " +
"AND #{through_reflection.table_name}.#{through_reflection.primary_key_name} = #{@owner.quoted_id}"
end