aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-16 10:29:13 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-20 13:56:04 -0800
commitb8153fd5a18441567f787a33ca882acb3bb5088a (patch)
tree54fcc3863416a35bff709a88fcfe2e2c91d182fc /activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
parent9863d8a5f6576ab10df51230c0531cec8d4468f9 (diff)
downloadrails-b8153fd5a18441567f787a33ca882acb3bb5088a.tar.gz
rails-b8153fd5a18441567f787a33ca882acb3bb5088a.tar.bz2
rails-b8153fd5a18441567f787a33ca882acb3bb5088a.zip
Fix problem where wrong keys are used in JoinAssociation when an association goes :through a belongs_to [#2801 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb21
1 files changed, 12 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
index 694778008b..02707dfae1 100644
--- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
@@ -190,17 +190,20 @@ module ActiveRecord
).alias @aliased_join_table_name
jt_conditions = []
- jt_foreign_key = first_key = second_key = nil
+ first_key = second_key = nil
- if through_reflection.options[:as] # has_many :through against a polymorphic join
- as_key = through_reflection.options[:as].to_s
- jt_foreign_key = as_key + '_id'
-
- jt_conditions <<
- join_table[as_key + '_type'].
- eq(parent.active_record.base_class.name)
+ if through_reflection.macro == :belongs_to
+ jt_primary_key = through_reflection.primary_key_name
+ jt_foreign_key = through_reflection.association_primary_key
else
+ jt_primary_key = through_reflection.active_record_primary_key
jt_foreign_key = through_reflection.primary_key_name
+
+ if through_reflection.options[:as] # has_many :through against a polymorphic join
+ jt_conditions <<
+ join_table["#{through_reflection.options[:as]}_type"].
+ eq(parent.active_record.base_class.name)
+ end
end
case source_reflection.macro
@@ -233,7 +236,7 @@ module ActiveRecord
end
jt_conditions <<
- parent_table[parent.primary_key].
+ parent_table[jt_primary_key].
eq(join_table[jt_foreign_key])
if through_reflection.options[:conditions]