diff --git a/lib/bullet/active_record4.rb b/lib/bullet/active_record4.rb index 9f1aced5..100efca1 100644 --- a/lib/bullet/active_record4.rb +++ b/lib/bullet/active_record4.rb @@ -107,12 +107,17 @@ def construct_association(record, join, row) result = origin_construct_association(record, join, row) if Bullet.start? - associations = join.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [join.reflection.name] + if join.reflection.nested? + associations << join.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end result diff --git a/lib/bullet/active_record41.rb b/lib/bullet/active_record41.rb index d4955ef2..96362348 100644 --- a/lib/bullet/active_record41.rb +++ b/lib/bullet/active_record41.rb @@ -110,12 +110,17 @@ def construct_model(record, node, row, model_cache, id, aliases) result = origin_construct_model(record, node, row, model_cache, id, aliases) if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.nested? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record42.rb b/lib/bullet/active_record42.rb index 1d28d971..c9632957 100644 --- a/lib/bullet/active_record42.rb +++ b/lib/bullet/active_record42.rb @@ -129,12 +129,17 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.nested? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -147,12 +152,17 @@ def construct_model(record, node, row, model_cache, id, aliases) result = origin_construct_model(record, node, row, model_cache, id, aliases) if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.nested? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record5.rb b/lib/bullet/active_record5.rb index bdd2b106..c3336bac 100644 --- a/lib/bullet/active_record5.rb +++ b/lib/bullet/active_record5.rb @@ -138,12 +138,17 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -156,12 +161,17 @@ def construct_model(record, node, row, model_cache, id, aliases) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record52.rb b/lib/bullet/active_record52.rb index 22378c06..b3e142e6 100644 --- a/lib/bullet/active_record52.rb +++ b/lib/bullet/active_record52.rb @@ -101,12 +101,17 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -119,12 +124,17 @@ def construct_model(record, node, row, model_cache, id, aliases) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record60.rb b/lib/bullet/active_record60.rb index ffca054d..419adf09 100644 --- a/lib/bullet/active_record60.rb +++ b/lib/bullet/active_record60.rb @@ -128,12 +128,17 @@ def construct(ar_parent, parent, row, seen, model_cache) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -146,12 +151,17 @@ def construct_model(record, node, row, model_cache, id) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record61.rb b/lib/bullet/active_record61.rb index 57cf89c3..355b7b7c 100644 --- a/lib/bullet/active_record61.rb +++ b/lib/bullet/active_record61.rb @@ -128,12 +128,17 @@ def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -146,12 +151,17 @@ def construct_model(record, node, row, model_cache, id, strict_loading_value) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record70.rb b/lib/bullet/active_record70.rb index d82b626b..2c6a0fde 100644 --- a/lib/bullet/active_record70.rb +++ b/lib/bullet/active_record70.rb @@ -137,12 +137,17 @@ def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -155,12 +160,17 @@ def construct_model(record, node, row, model_cache, id, strict_loading_value) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/lib/bullet/active_record71.rb b/lib/bullet/active_record71.rb index d82b626b..2c6a0fde 100644 --- a/lib/bullet/active_record71.rb +++ b/lib/bullet/active_record71.rb @@ -137,12 +137,17 @@ def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value) id = row[key] next unless id.nil? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(ar_parent, associations) - Bullet::Detector::NPlusOneQuery.call_association(ar_parent, associations) - @bullet_eager_loadings[ar_parent.class] ||= {} - @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new - @bullet_eager_loadings[ar_parent.class][ar_parent] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(ar_parent, association) + Bullet::Detector::NPlusOneQuery.call_association(ar_parent, association) + @bullet_eager_loadings[ar_parent.class] ||= {} + @bullet_eager_loadings[ar_parent.class][ar_parent] ||= Set.new + @bullet_eager_loadings[ar_parent.class][ar_parent] << association + end end end end @@ -155,12 +160,17 @@ def construct_model(record, node, row, model_cache, id, strict_loading_value) result = super if Bullet.start? - associations = node.reflection.name - Bullet::Detector::Association.add_object_associations(record, associations) - Bullet::Detector::NPlusOneQuery.call_association(record, associations) - @bullet_eager_loadings[record.class] ||= {} - @bullet_eager_loadings[record.class][record] ||= Set.new - @bullet_eager_loadings[record.class][record] << associations + associations = [node.reflection.name] + if node.reflection.through_reflection? + associations << node.reflection.through_reflection.name + end + associations.each do |association| + Bullet::Detector::Association.add_object_associations(record, association) + Bullet::Detector::NPlusOneQuery.call_association(record, association) + @bullet_eager_loadings[record.class] ||= {} + @bullet_eager_loadings[record.class][record] ||= Set.new + @bullet_eager_loadings[record.class][record] << association + end end result diff --git a/spec/integration/active_record/association_spec.rb b/spec/integration/active_record/association_spec.rb index 8c25d8cf..c913c5a3 100644 --- a/spec/integration/active_record/association_spec.rb +++ b/spec/integration/active_record/association_spec.rb @@ -638,7 +638,7 @@ expect(Bullet::Detector::Association).to be_detecting_unpreloaded_association_for(User, :submission_attachment) end - it 'should detect preload associations' do + it 'should not detect preload associations with includes' do User.includes(:submission_attachment).each { |user| user.submission_attachment.file_name } Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations @@ -646,6 +646,14 @@ expect(Bullet::Detector::Association).to be_completely_preloading_associations end + it 'should detect preload associations with eager_load' do + User.eager_load(:submission_attachment).each { |user| user.submission_attachment.file_name } + Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations + expect(Bullet::Detector::Association).not_to be_has_unused_preload_associations + + expect(Bullet::Detector::Association).to be_completely_preloading_associations + end + it 'should not detect preload associations' do User.all.map(&:name) Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations