1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

bypass preloading for ids_reader

when fetching ids for a collection, bypass preloading
to avoid the unnecessary performance overhead
This commit is contained in:
Sergey Nartimov 2011-12-17 19:50:47 +03:00
parent 7bde2e2578
commit 109db5a550
2 changed files with 16 additions and 2 deletions

View file

@ -49,10 +49,18 @@ module ActiveRecord
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 file

@ -528,6 +528,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
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)