Skip to content

Commit

Permalink
consider ThroughAssociation at SingularAssociation like CollectionAss…
Browse files Browse the repository at this point in the history
…ociation
  • Loading branch information
kehra committed Jan 16, 2021
1 parent 9d16144 commit e063183
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/bullet/active_record52.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ def target

if Bullet.start?
if owner.class.name !~ /^HABTM_/ && !@inversed
if is_a? ::ActiveRecord::Associations::ThroughAssociation
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
association = owner.association reflection.through_reflection.name
Array(association.target).each do |through_record|
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
end

if reflection.through_reflection != through_reflection
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
end
end
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)

if Bullet::Detector::NPlusOneQuery.impossible?(owner)
Expand Down
11 changes: 11 additions & 0 deletions lib/bullet/active_record60.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ def target

if Bullet.start?
if owner.class.name !~ /^HABTM_/ && !@inversed
if is_a? ::ActiveRecord::Associations::ThroughAssociation
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
association = owner.association(reflection.through_reflection.name)
Array(association.target).each do |through_record|
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
end

if reflection.through_reflection != through_reflection
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
end
end
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)

if Bullet::Detector::NPlusOneQuery.impossible?(owner)
Expand Down
11 changes: 11 additions & 0 deletions lib/bullet/active_record61.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ def target

if Bullet.start?
if owner.class.name !~ /^HABTM_/ && !@inversed
if is_a? ::ActiveRecord::Associations::ThroughAssociation
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
association = owner.association(reflection.through_reflection.name)
Array(association.target).each do |through_record|
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
end

if reflection.through_reflection != through_reflection
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
end
end
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)

if Bullet::Detector::NPlusOneQuery.impossible?(owner)
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/active_record/association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
end

describe Bullet::Detector::Association, 'has_one :through' do
context 'user => submission => attachment' do
context 'user => attachment' do
it 'should detect non preload associations' do
User.all.each { |user| user.submission_attachment.file_name }
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
Expand Down

0 comments on commit e063183

Please sign in to comment.