aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-19 15:24:30 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-19 15:24:30 +0100
commit0ceb34295501a797c9e549c581ecee17f837f01c (patch)
tree99ba5d6e32716b6539decfa987c546166c32a354 /activerecord/lib/active_record
parent9ff5fdeda99b3d8c5148d4c40956842518d1c788 (diff)
downloadrails-0ceb34295501a797c9e549c581ecee17f837f01c.tar.gz
rails-0ceb34295501a797c9e549c581ecee17f837f01c.tar.bz2
rails-0ceb34295501a797c9e549c581ecee17f837f01c.zip
Bugfix/refactoring
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb12
-rw-r--r--activerecord/lib/active_record/associations/through_association_scope.rb10
-rw-r--r--activerecord/lib/active_record/reflection.rb9
3 files changed, 20 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 29f1c7b81d..9e000f2aae 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -2207,18 +2207,18 @@ module ActiveRecord
else
case reflection.source_reflection.macro
when :belongs_to
- key = reflection.source_reflection.association_primary_key
- foreign_key = reflection.source_reflection.primary_key_name
+ key = reflection.association_primary_key
+ foreign_key = reflection.primary_key_name
conditions << source_type_conditions(reflection, foreign_table)
when :has_many, :has_one
- key = reflection.source_reflection.primary_key_name
+ key = reflection.primary_key_name
foreign_key = reflection.source_reflection.active_record_primary_key
when :has_and_belongs_to_many
table, join_table = table
- join_key = reflection.source_reflection.primary_key_name
- join_foreign_key = reflection.source_reflection.klass.primary_key
+ join_key = reflection.primary_key_name
+ join_foreign_key = reflection.klass.primary_key
relation = relation.join(join_table, join_type).on(
join_table[join_key].
@@ -2228,7 +2228,7 @@ module ActiveRecord
foreign_table = join_table
key = reflection.klass.primary_key
- foreign_key = reflection.source_reflection.association_foreign_key
+ foreign_key = reflection.association_foreign_key
end
end
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb
index 2b2229f01f..1365851337 100644
--- a/activerecord/lib/active_record/associations/through_association_scope.rb
+++ b/activerecord/lib/active_record/associations/through_association_scope.rb
@@ -108,8 +108,8 @@ module ActiveRecord
when :belongs_to
joins << inner_join_sql(
right_table_and_alias,
- table_aliases[left], left.source_reflection.association_primary_key,
- table_aliases[right], left.source_reflection.primary_key_name,
+ table_aliases[left], left.association_primary_key,
+ table_aliases[right], left.primary_key_name,
source_type_conditions(left),
reflection_conditions(right_index)
)
@@ -123,7 +123,7 @@ module ActiveRecord
joins << inner_join_sql(
right_table_and_alias,
- table_aliases[left], left.source_reflection.primary_key_name,
+ table_aliases[left], left.primary_key_name,
right_table, left.source_reflection.active_record_primary_key,
polymorphic_conditions(left, left.source_reflection),
reflection_conditions(right_index)
@@ -148,12 +148,12 @@ module ActiveRecord
join_table
),
left_table, left.klass.primary_key,
- join_table, left.source_reflection.association_foreign_key
+ join_table, left.association_foreign_key
)
joins << inner_join_sql(
right_table_and_alias,
- join_table, left.source_reflection.primary_key_name,
+ join_table, left.primary_key_name,
table_aliases[right], right.klass.primary_key,
reflection_conditions(right_index)
)
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 6078191773..7ce2bbb8ae 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -357,6 +357,8 @@ module ActiveRecord
# Holds all the meta-data about a :through association as it was specified
# in the Active Record class.
class ThroughReflection < AssociationReflection #:nodoc:
+ delegate :primary_key_name, :association_foreign_key, :to => :source_reflection
+
# Gets the source of the through reflection. It checks both a singularized
# and pluralized form for <tt>:belongs_to</tt> or <tt>:has_many</tt>.
#
@@ -451,6 +453,13 @@ module ActiveRecord
def nested?
through_reflection_chain.length > 2
end
+
+ # We want to use the klass from this reflection, rather than just delegate straight to
+ # the source_reflection, because the source_reflection may be polymorphic. We still
+ # need to respect the source_reflection's :primary_key option, though.
+ def association_primary_key
+ @association_primary_key ||= source_reflection.options[:primary_key] || klass.primary_key
+ end
# Gets an array of possible <tt>:through</tt> source reflection names:
#