aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-02-14 20:39:51 +0000
committerJon Leighton <j@jonathanleighton.com>2011-02-18 00:00:13 +0000
commitf0b98050296b57d95dbc789f8e52fa82499d151a (patch)
tree817c7fa99ab5119b6db4a95f5fe2f415975cae54 /activerecord/lib/active_record
parentb131cfba055bfbf25f3785c5235c1edbf04ff9f1 (diff)
downloadrails-f0b98050296b57d95dbc789f8e52fa82499d151a.tar.gz
rails-f0b98050296b57d95dbc789f8e52fa82499d151a.tar.bz2
rails-f0b98050296b57d95dbc789f8e52fa82499d151a.zip
Ensure that association_ids uses the correct attribute where the association is a has_many :through with a :primary_key option on the source reflection. [#6376 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations.rb7
-rw-r--r--activerecord/lib/active_record/reflection.rb4
2 files changed, 9 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index a2db592c7d..529d8ca2d3 100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1582,9 +1582,12 @@ module ActiveRecord
redefine_method("#{reflection.name.to_s.singularize}_ids") do
if send(reflection.name).loaded? || reflection.options[:finder_sql]
- send(reflection.name).map { |r| r.id }
+ records = send(reflection.name)
+ records.map { |r| r.send(reflection.association_primary_key) }
else
- send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").except(:includes).map! { |r| r.id }
+ column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
+ records = send(reflection.name).select(column).except(:includes)
+ records.map! { |r| r.send(reflection.association_primary_key) }
end
end
end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index ceeb0ec39d..0a0e8d650c 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -394,6 +394,10 @@ module ActiveRecord
@source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }
end
+ def association_primary_key
+ source_reflection.association_primary_key
+ end
+
def check_validity!
if through_reflection.nil?
raise HasManyThroughAssociationNotFoundError.new(active_record.name, self)