aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/through_association_scope.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-15 23:27:15 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-15 17:50:14 -0800
commit14b880fd035fcdf807051398674c9aa89bd3b4d3 (patch)
tree6f80999405dea289391abfdf4552cea407d9cd45 /activerecord/lib/active_record/associations/through_association_scope.rb
parent09ddca67acbb88e2fdd7300670839cbf647b2694 (diff)
downloadrails-14b880fd035fcdf807051398674c9aa89bd3b4d3.tar.gz
rails-14b880fd035fcdf807051398674c9aa89bd3b4d3.tar.bz2
rails-14b880fd035fcdf807051398674c9aa89bd3b4d3.zip
Fix various issues with the :primary_key option in :through associations [#2421 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record/associations/through_association_scope.rb')
-rw-r--r--activerecord/lib/active_record/associations/through_association_scope.rb26
1 files changed, 9 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/associations/through_association_scope.rb b/activerecord/lib/active_record/associations/through_association_scope.rb
index 2ecd0f054a..5dc5b0c048 100644
--- a/activerecord/lib/active_record/associations/through_association_scope.rb
+++ b/activerecord/lib/active_record/associations/through_association_scope.rb
@@ -39,22 +39,22 @@ module ActiveRecord
# Build SQL conditions from attributes, qualified by table name.
def construct_conditions
table = aliased_through_table
- conditions = construct_quoted_owner_attributes(@reflection.through_reflection).map do |attr, value|
+ conditions = construct_owner_attributes(@reflection.through_reflection).map do |attr, value|
table[attr].eq(value)
end
conditions << Arel.sql(sql_conditions) if sql_conditions
table.create_and(conditions)
end
- # Associate attributes pointing to owner, quoted.
- def construct_quoted_owner_attributes(reflection)
+ # Associate attributes pointing to owner
+ def construct_owner_attributes(reflection)
if as = reflection.options[:as]
- { "#{as}_id" => @owner.id,
+ { "#{as}_id" => @owner[reflection.active_record_primary_key],
"#{as}_type" => @owner.class.base_class.name }
elsif reflection.macro == :belongs_to
{ reflection.klass.primary_key => @owner[reflection.primary_key_name] }
else
- { reflection.primary_key_name => @owner.id }
+ { reflection.primary_key_name => @owner[reflection.active_record_primary_key] }
end
end
@@ -74,7 +74,8 @@ module ActiveRecord
conditions = []
if @reflection.source_reflection.macro == :belongs_to
- reflection_primary_key = @reflection.klass.primary_key
+ reflection_primary_key = @reflection.source_reflection.options[:primary_key] ||
+ @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
if @reflection.options[:source_type]
column = @reflection.source_reflection.options[:foreign_type]
@@ -83,7 +84,8 @@ module ActiveRecord
end
else
reflection_primary_key = @reflection.source_reflection.primary_key_name
- source_primary_key = @reflection.through_reflection.klass.primary_key
+ source_primary_key = @reflection.source_reflection.options[:primary_key] ||
+ @reflection.through_reflection.klass.primary_key
if @reflection.source_reflection.options[:as]
column = "#{@reflection.source_reflection.options[:as]}_type"
conditions <<
@@ -99,16 +101,6 @@ module ActiveRecord
right.create_on(right.create_and(conditions)))
end
- # Construct attributes for associate pointing to owner.
- def construct_owner_attributes(reflection)
- if as = reflection.options[:as]
- { "#{as}_id" => @owner.id,
- "#{as}_type" => @owner.class.base_class.name }
- else
- { reflection.primary_key_name => @owner.id }
- end
- end
-
# Construct attributes for :through pointing to owner and associate.
def construct_join_attributes(associate)
# TODO: revisit this to allow it for deletion, supposing dependent option is supported