Skip to content

Commit

Permalink
Do not report association queries immediately after object creation t…
Browse files Browse the repository at this point in the history
…o require a preload.
  • Loading branch information
lowjoel committed Jul 24, 2015
1 parent 4475148 commit 52b3739
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/bullet/active_record42.rb
Expand Up @@ -21,6 +21,16 @@ def find(*args)
end
end

::ActiveRecord::Persistence.class_eval do
def save_with_bullet(*args, &proc)
was_new_record = new_record?
save_without_bullet(*args, &proc).tap do |result|
Bullet::Detector::NPlusOneQuery.add_impossible_object(self) if result && was_new_record
end
end
alias_method_chain :save, :bullet
end

::ActiveRecord::Relation.class_eval do
alias_method :origin_to_a, :to_a
# if select a collection of objects, then these objects have possible to cause N+1 query.
Expand Down
21 changes: 21 additions & 0 deletions spec/integration/active_record4/association_spec.rb
Expand Up @@ -560,6 +560,27 @@
end
end

describe Bullet::Detector::Association, "query immediately after creation" do
context "document => children" do
it 'should not detect non preload associations' do
document1 = Document.new
document1.children.build
document1.save

document2 = Document.new(parent: document1)
document2.save
document2.parent

document1.children.each.first

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
end
end

describe Bullet::Detector::Association, "STI" do
context "page => author" do
it "should detect non preload associations" do
Expand Down

0 comments on commit 52b3739

Please sign in to comment.