aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-19 14:14:06 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-19 14:14:06 +0100
commit01838636c6136d9a649ace71db61bb7990f9bd82 (patch)
tree2f307e4dd6843588ab19f7d41615a04465560fff /activerecord/lib/active_record
parent596cc3b2329a9cc4a30c95c157ce36b2d08975df (diff)
downloadrails-01838636c6136d9a649ace71db61bb7990f9bd82.tar.gz
rails-01838636c6136d9a649ace71db61bb7990f9bd82.tar.bz2
rails-01838636c6136d9a649ace71db61bb7990f9bd82.zip
Support for :primary_key option on the source reflection of a through association, where the source is a has_one or has_many
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb10
-rw-r--r--activerecord/lib/active_record/associations/through_association_scope.rb4
-rw-r--r--activerecord/lib/active_record/reflection.rb8
3 files changed, 14 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 75e5eb8ee4..29f1c7b81d 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -2173,13 +2173,11 @@ module ActiveRecord
if reflection.source_reflection.nil?
case reflection.macro
when :belongs_to
- key = reflection.options[:primary_key] ||
- reflection.klass.primary_key
+ key = reflection.association_primary_key
foreign_key = reflection.primary_key_name
when :has_many, :has_one
key = reflection.primary_key_name
- foreign_key = reflection.options[:primary_key] ||
- reflection.active_record.primary_key
+ foreign_key = reflection.active_record_primary_key
conditions << polymorphic_conditions(reflection, table)
when :has_and_belongs_to_many
@@ -2209,13 +2207,13 @@ module ActiveRecord
else
case reflection.source_reflection.macro
when :belongs_to
- key = reflection.klass.primary_key
+ key = reflection.source_reflection.association_primary_key
foreign_key = reflection.source_reflection.primary_key_name
conditions << source_type_conditions(reflection, foreign_table)
when :has_many, :has_one
key = reflection.source_reflection.primary_key_name
- foreign_key = reflection.source_reflection.klass.primary_key
+ foreign_key = reflection.source_reflection.active_record_primary_key
when :has_and_belongs_to_many
table, join_table = table
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb
index 86ceb1a204..2b2229f01f 100644
--- a/activerecord/lib/active_record/associations/through_association_scope.rb
+++ b/activerecord/lib/active_record/associations/through_association_scope.rb
@@ -108,7 +108,7 @@ module ActiveRecord
when :belongs_to
joins << inner_join_sql(
right_table_and_alias,
- table_aliases[left], left.klass.primary_key,
+ table_aliases[left], left.source_reflection.association_primary_key,
table_aliases[right], left.source_reflection.primary_key_name,
source_type_conditions(left),
reflection_conditions(right_index)
@@ -124,7 +124,7 @@ module ActiveRecord
joins << inner_join_sql(
right_table_and_alias,
table_aliases[left], left.source_reflection.primary_key_name,
- right_table, right.klass.primary_key,
+ right_table, left.source_reflection.active_record_primary_key,
polymorphic_conditions(left, left.source_reflection),
reflection_conditions(right_index)
)
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index d8bd6c9873..a46597e497 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -217,6 +217,14 @@ module ActiveRecord
def association_foreign_key
@association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end
+
+ def association_primary_key
+ @association_primary_key ||= @options[:primary_key] || klass.primary_key
+ end
+
+ def active_record_primary_key
+ @active_record_primary_key ||= @options[:primary_key] || active_record.primary_key
+ end
def counter_cache_column
if options[:counter_cache] == true