aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-07-13 16:25:33 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-07-13 16:29:19 +0100
commit373b053dc8b99dac1abc3879a17a2bf8c30302b5 (patch)
tree04e194875edb6b9e5fbdcd2907e5439fba5a0eb8 /activerecord/lib
parentc863388039ff6e97447be86184d41fbf30f0f389 (diff)
downloadrails-373b053dc8b99dac1abc3879a17a2bf8c30302b5.tar.gz
rails-373b053dc8b99dac1abc3879a17a2bf8c30302b5.tar.bz2
rails-373b053dc8b99dac1abc3879a17a2bf8c30302b5.zip
Optimize <association>_ids for hm:t with belongs_to source
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 10ecd068d3..1b884fd2ab 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1273,9 +1273,16 @@ module ActiveRecord
if send(reflection.name).loaded? || reflection.options[:finder_sql]
send(reflection.name).map(&:id)
else
- send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id)
+ if reflection.through_reflection && reflection.source_reflection.belongs_to?
+ through = reflection.through_reflection
+ primary_key = reflection.source_reflection.primary_key_name
+ send(through.name).all(:select => "DISTINCT #{through.quoted_table_name}.#{primary_key}").map(&:"#{primary_key}")
+ else
+ send(reflection.name).all(:select => "#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map(&:id)
+ end
end
end
+
end
def collection_accessor_methods(reflection, association_proxy_class, writer = true)