Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4014 from lest/bypass-preloading-for-ids-reader

bypass preloading for ids_reader
  • Loading branch information...
commit de2306f10cc8c0f91bf9fe3ef5c7e3daee9035f6 2 parents 81c67c1 + 109db5a
@jonleighton jonleighton authored
View
12 activerecord/lib/active_record/associations/collection_association.rb
@@ -49,10 +49,18 @@ def ids_reader
end
else
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
+ relation = scoped
- scoped.select(column).map! do |record|
- record.send(reflection.association_primary_key)
+ including = (relation.eager_load_values + relation.includes_values).uniq
+
+ if including.any?
+ join_dependency = ActiveRecord::Associations::JoinDependency.new(reflection.klass, including, [])
+ relation = join_dependency.join_associations.inject(relation) do |r, association|
+ association.join_relation(r)
+ end
end
+
+ relation.uniq.pluck(column)
end
end
View
6 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -528,6 +528,12 @@ def test_get_ids
assert_equal [posts(:welcome).id, posts(:authorless).id].sort, people(:michael).post_ids.sort
end
+ def test_get_ids_for_has_many_through_with_conditions_should_not_preload
+ Tagging.create!(:taggable_type => 'Post', :taggable_id => posts(:welcome).id, :tag => tags(:misc))
+ ActiveRecord::Associations::Preloader.expects(:new).never
+ posts(:welcome).misc_tag_ids
+ end
+
def test_get_ids_for_loaded_associations
person = people(:michael)
person.posts(true)
Please sign in to comment.
Something went wrong with that request. Please try again.