[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7677":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":17,"stars30d":18,"stars90d":16,"forks30d":16,"starsTrendScore":19,"compositeScore":20,"rankGlobal":10,"rankLanguage":10,"license":21,"archived":22,"fork":22,"defaultBranch":23,"hasWiki":24,"hasPages":22,"topics":25,"createdAt":10,"pushedAt":10,"updatedAt":26,"readmeContent":27,"aiSummary":28,"trendingCount":16,"starSnapshotCount":16,"syncStatus":19,"lastSyncTime":29,"discoverSource":30},7677,"bullet","flyerhzm\u002Fbullet","flyerhzm","help to kill N+1 queries and unused eager loading","",null,"Ruby",7328,452,66,1,0,4,16,2,38.97,"MIT License",false,"main",true,[],"2026-06-12 02:01:43","# Bullet\n\n![Main workflow](https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Fbullet\u002Factions\u002Fworkflows\u002Fmain.yml\u002Fbadge.svg)\n[![Gem Version](https:\u002F\u002Fbadge.fury.io\u002Frb\u002Fbullet.svg)](http:\u002F\u002Fbadge.fury.io\u002Frb\u002Fbullet)\n[![AwesomeCode Status for flyerhzm\u002Fbullet](https:\u002F\u002Fawesomecode.io\u002Fprojects\u002F6755235b-e2c1-459e-bf92-b8b13d0c0472\u002Fstatus)](https:\u002F\u002Fawesomecode.io\u002Frepos\u002Fflyerhzm\u002Fbullet)\n[![Coderwall Endorse](https:\u002F\u002Fcoderwall.com\u002Fflyerhzm\u002Fendorsecount.png)](https:\u002F\u002Fcoderwall.com\u002Fflyerhzm)\n\nThe Bullet gem is designed to help you increase your application's performance by reducing the number of queries it makes. It will watch your queries while you develop your application and notify you when you should add eager loading (N+1 queries), when you're using eager loading that isn't necessary and when you should use counter cache.\n\nBest practice is to use Bullet in development mode or custom mode (staging, profile, etc.). The last thing you want is your clients getting alerts about how lazy you are.\n\nBullet gem now supports **activerecord** >= 4.0 and **mongoid** >= 4.0.\n\nIf you use activerecord 2.x, please use bullet \u003C= 4.5.0\n\nIf you use activerecord 3.x, please use bullet \u003C 5.5.0\n\n## External Introduction\n\n* [https:\u002F\u002Frubyonrails.org\u002F2009\u002F10\u002F22\u002Fcommunity-highlights](https:\u002F\u002Frubyonrails.org\u002F2009\u002F10\u002F22\u002Fcommunity-highlights)\n* [https:\u002F\u002Fwww.shakacode.com\u002Fblog\u002Feliminate-N-1-queries-with-bullet\u002F](https:\u002F\u002Fwww.shakacode.com\u002Fblog\u002Feliminate-N-1-queries-with-bullet\u002F)\n* [https:\u002F\u002Freinteractive.com\u002Farticles\u002Fruby-on-rails-community\u002Felevating-your-code-quality-bullet-gem-and-query-prevention-in-testing](https:\u002F\u002Freinteractive.com\u002Farticles\u002Fruby-on-rails-community\u002Felevating-your-code-quality-bullet-gem-and-query-prevention-in-testing)\n\n## Install\n\nYou can install it as a gem:\n\n```\ngem install bullet\n```\n\nor add it into a Gemfile (Bundler):\n\n\n```ruby\ngem 'bullet', group: 'development'\n```\n\nenable the Bullet gem with generate command\n\n```ruby\nbundle exec rails g bullet:install\n```\nThe generate command will auto generate the default configuration and may ask to include in the test environment as well. See below for custom configuration.\n\n**Note**: make sure `bullet` gem is added after activerecord (rails) and\nmongoid.\n\n## Configuration\n\nBullet won't enable any notification systems unless you tell it to explicitly. Append to\n`config\u002Fenvironments\u002Fdevelopment.rb` initializer with the following code:\n\n```ruby\nconfig.after_initialize do\n  Bullet.enable = true\n  Bullet.sentry = true\n  Bullet.alert = true\n  Bullet.bullet_logger = true\n  Bullet.console = true\n  Bullet.xmpp = { :account  => 'bullets_account@jabber.org',\n                  :password => 'bullets_password_for_jabber',\n                  :receiver => 'your_account@jabber.org',\n                  :show_online_status => true }\n  Bullet.rails_logger = true\n  Bullet.honeybadger = true\n  Bullet.bugsnag = true\n  Bullet.appsignal = true\n  Bullet.airbrake = true\n  Bullet.rollbar = true\n  Bullet.add_footer = true\n  Bullet.skip_html_injection = false\n  Bullet.skip_http_headers = false\n  Bullet.stacktrace_includes = [ 'your_gem', 'your_middleware' ]\n  Bullet.stacktrace_excludes = [ 'their_gem', 'their_middleware', ['my_file.rb', 'my_method'], ['my_file.rb', 16..20] ]\n  Bullet.slack = { webhook_url: 'http:\u002F\u002Fsome.slack.url', channel: '#default', username: 'notifier' }\n  Bullet.opentelemetry = true\n  Bullet.raise = false\n  Bullet.always_append_html_body = false\n  Bullet.skip_user_in_notification = false\nend\n```\n\nThe notifier of Bullet is a wrap of [uniform_notifier](https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Funiform_notifier)\n\nThe code above will enable all of the Bullet notification systems:\n* `Bullet.enable`: enable Bullet gem, otherwise do nothing\n* `Bullet.sentry`: add notifications to sentry\n* `Bullet.alert`: pop up a JavaScript alert in the browser\n* `Bullet.bullet_logger`: log to the Bullet log file (Rails.root\u002Flog\u002Fbullet.log)\n* `Bullet.console`: log warnings to your browser's console.log (Safari\u002FWebkit browsers or Firefox w\u002FFirebug installed)\n* `Bullet.xmpp`: send XMPP\u002FJabber notifications to the receiver indicated. Note that the code will currently not handle the adding of contacts, so you will need to make both accounts indicated know each other manually before you will receive any notifications. If you restart the development server frequently, the 'coming online' sound for the Bullet account may start to annoy - in this case set :show_online_status to false; you will still get notifications, but the Bullet account won't announce it's online status anymore.\n* `Bullet.rails_logger`: add warnings directly to the Rails log\n* `Bullet.honeybadger`: add notifications to Honeybadger\n* `Bullet.bugsnag`: add notifications to bugsnag\n* `Bullet.appsignal`: add notifications to AppSignal\n* `Bullet.airbrake`: add notifications to airbrake\n* `Bullet.rollbar`: add notifications to rollbar\n* `Bullet.add_footer`: adds the details in the bottom left corner of the page. Double click the footer or use close button to hide footer.\n* `Bullet.skip_html_injection`: prevents Bullet from injecting code into the returned HTML. This must be false for receiving alerts, showing the footer or console logging.\n* `Bullet.skip_http_headers`: don't add headers to API requests, and remove the javascript that relies on them. Note that this prevents bullet from logging warnings to the browser console or updating the footer.\n* `Bullet.stacktrace_includes`: include paths with any of these substrings in the stack trace, even if they are not in your main app\n* `Bullet.stacktrace_excludes`: ignore paths with any of these substrings in the stack trace, even if they are not in your main app.\n   Each item can be a string (match substring), a regex, or an array where the first item is a path to match, and the second\n   item is a line number, a Range of line numbers, or a (bare) method name, to exclude only particular lines in a file.\n* `Bullet.slack`: add notifications to slack\n* `Bullet.opentelemetry`: add notifications to OpenTelemetry\n* `Bullet.raise`: raise errors, useful for making your specs fail unless they have optimized queries\n* `Bullet.always_append_html_body`: always append the html body even if no notifications are present. Note: `console` or `add_footer` must also be true. Useful for Single Page Applications where the initial page load might not have any notifications present.\n* `Bullet.skip_user_in_notification`: exclude the OS user (`whoami`) from notifications.\n\n\nBullet also allows you to disable any of its detectors.\n\n```ruby\n# Each of these settings defaults to true\n\n# Detect N+1 queries\nBullet.n_plus_one_query_enable     = false\n\n# Detect eager-loaded associations which are not used\nBullet.unused_eager_loading_enable = false\n\n# Detect unnecessary COUNT queries which could be avoided\n# with a counter_cache\nBullet.counter_cache_enable        = false\n```\n\n## Safe list\n\nSometimes Bullet may notify you of query problems you don't care to fix, or\nwhich come from outside your code. You can add them to a safe list to ignore them:\n\n```ruby\nBullet.add_safelist :type => :n_plus_one_query, :class_name => \"Post\", :association => :comments\nBullet.add_safelist :type => :unused_eager_loading, :class_name => \"Post\", :association => :comments\nBullet.add_safelist :type => :counter_cache, :class_name => \"Country\", :association => :cities\n```\n\nIf you want to skip bullet in some specific controller actions, you can\ndo like\n\n```ruby\nclass ApplicationController \u003C ActionController::Base\n  around_action :skip_bullet, if: -> { defined?(Bullet) }\n\n  def skip_bullet\n    previous_value = Bullet.enable?\n    Bullet.enable = false\n    yield\n  ensure\n    Bullet.enable = previous_value\n  end\nend\n```\n\n## Log\n\nThe Bullet log `log\u002Fbullet.log` will look something like this:\n\n* N+1 Query:\n\n```\n2009-08-25 20:40:17[INFO] USE eager loading detected:\n  Post => [:comments]·\n  Add to your query: .includes([:comments])\n2009-08-25 20:40:17[INFO] Call stack\n  \u002FUsers\u002Frichard\u002FDownloads\u002Ftest\u002Fapp\u002Fviews\u002Fposts\u002Findex.html.erb:8:in `each'\n  \u002FUsers\u002Frichard\u002FDownloads\u002Ftest\u002Fapp\u002Fcontrollers\u002Fposts_controller.rb:7:in `index'\n```\n\nThe first log entry is a notification that N+1 queries have been encountered. The remaining entry is a stack trace so you can find exactly where the queries were invoked in your code, and fix them.\n\n* Unused eager loading:\n\n```\n2009-08-25 20:53:56[INFO] AVOID eager loading detected\n  Post => [:comments]·\n  Remove from your query: .includes([:comments])\n2009-08-25 20:53:56[INFO] Call stack\n```\n\nThese lines are notifications that unused eager loadings have been encountered.\n\n* Need counter cache:\n\n```\n2009-09-11 09:46:50[INFO] Need Counter Cache\n  Post => [:comments]\n```\n\n## XMPP\u002FJabber and Airbrake Support\n\nsee [https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Funiform_notifier](https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Funiform_notifier)\n\n## Growl Support\n\nGrowl support is dropped from uniform_notifier 1.16.0, if you still want it, please use uniform_notifier 1.15.0.\n\n## URL query control\n\nYou can add the URL query parameter `skip_html_injection` to make the current HTML request behave as if `Bullet.skip_html_injection` is enabled,\ne.g. `http:\u002F\u002Flocalhost:3000\u002Fposts?skip_html_injection=true`\n\n## Important\n\nIf you find Bullet does not work for you, *please disable your browser's cache*.\n\n## Advanced\n\n### Work with ActiveJob\n\nInclude `Bullet::ActiveJob` in your `ApplicationJob`.\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  include Bullet::ActiveJob if Rails.env.development?\nend\n```\n\n### Work with other background job solution\n\nUse the Bullet.profile method.\n\n```ruby\nclass ApplicationJob \u003C ActiveJob::Base\n  around_perform do |_job, block|\n    Bullet.profile do\n      block.call\n    end\n  end\nend\n```\n\n### Work with sinatra\n\nConfigure and use `Bullet::Rack`.\n\n```ruby\nconfigure :development do\n  Bullet.enable = true\n  Bullet.bullet_logger = true\n  use Bullet::Rack\nend\n```\n\nIf your application generates a Content-Security-Policy via a separate middleware, ensure that `Bullet::Rack` is loaded _before_ that middleware.\n\n### Run in tests\n\nFirst you need to enable Bullet in the test environment.\n\n```ruby\n# config\u002Fenvironments\u002Ftest.rb\nconfig.after_initialize do\n  Bullet.enable = true\n  Bullet.bullet_logger = true\n  Bullet.raise = true # raise an error if n+1 query occurs\nend\n```\n\nThen wrap each test in the Bullet api.\n\nWith RSpec:\n\n```ruby\n# spec\u002Frails_helper.rb\nRSpec.configure do |config|\n  config.before(:each) do\n    Bullet.start_request\n  end\n\n  config.after(:each) do\n    Bullet.perform_out_of_channel_notifications if Bullet.notification?\n    Bullet.end_request\n  end\nend\n```\n\nWith Minitest:\n\n```ruby\n# test\u002Ftest_helper.rb\nmodule ActiveSupport\n  class TestCase\n    def before_setup\n      Bullet.start_request\n      super\n    end\n\n    def after_teardown\n      super\n      Bullet.perform_out_of_channel_notifications if Bullet.notification?\n      Bullet.end_request\n    end\n  end\nend\n```\n\n## Debug Mode\n\nBullet outputs some details info, to enable debug mode, set\n`BULLET_DEBUG=true` env.\n\n## Contributors\n\n[https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Fbullet\u002Fcontributors](https:\u002F\u002Fgithub.com\u002Fflyerhzm\u002Fbullet\u002Fcontributors)\n\n## Demo\n\nBullet is designed to function as you browse through your application in development. To see it in action,\nyou can follow these steps to create, detect, and fix example query problems.\n\n1\\. Create an example application\n\n```\n$ rails new test_bullet\n$ cd test_bullet\n$ rails g scaffold post name:string\n$ rails g scaffold comment name:string post_id:integer\n$ bundle exec rails db:migrate\n```\n\n2\\. Change `app\u002Fmodels\u002Fpost.rb` and `app\u002Fmodels\u002Fcomment.rb`\n\n```ruby\nclass Post \u003C ApplicationRecord\n  has_many :comments\nend\n\nclass Comment \u003C ApplicationRecord\n  belongs_to :post\nend\n```\n\n3\\. Go to `rails c` and execute\n\n```ruby\npost1 = Post.create(:name => 'first')\npost2 = Post.create(:name => 'second')\npost1.comments.create(:name => 'first')\npost1.comments.create(:name => 'second')\npost2.comments.create(:name => 'third')\npost2.comments.create(:name => 'fourth')\n```\n\n4\\. Change the `app\u002Fviews\u002Fposts\u002Findex.html.erb` to produce a N+1 query\n\n```\n\u003C% @posts.each do |post| %>\n  \u003Ctr>\n    \u003Ctd>\u003C%= post.name %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= post.comments.map(&:name) %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Show', post %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Edit', edit_post_path(post) %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>\u003C\u002Ftd>\n  \u003C\u002Ftr>\n\u003C% end %>\n```\n\n5\\. Add the `bullet` gem to the `Gemfile`\n\n```ruby\ngem \"bullet\"\n```\n\nAnd run\n\n```\nbundle install\n```\n\n6\\. enable the Bullet gem with generate command\n\n```\nbundle exec rails g bullet:install\n```\n\n7\\. Start the server\n\n```\n$ rails s\n```\n\n8\\. Visit `http:\u002F\u002Flocalhost:3000\u002Fposts` in browser, and you will see a popup alert box that says\n\n```\nThe request has unused preload associations as follows:\nNone\nThe request has N+1 queries as follows:\nmodel: Post => associations: [comment]\n```\n\nwhich means there is a N+1 query from the Post object to its Comment association.\n\nIn the meantime, there's a log appended into `log\u002Fbullet.log` file\n\n```\n2010-03-07 14:12:18[INFO] N+1 Query in \u002Fposts\n  Post => [:comments]\n  Add to your finder: :include => [:comments]\n2010-03-07 14:12:18[INFO] N+1 Query method call stack\n  \u002Fhome\u002Fflyerhzm\u002FDownloads\u002Ftest_bullet\u002Fapp\u002Fviews\u002Fposts\u002Findex.html.erb:14:in `_render_template__600522146_80203160_0'\n  \u002Fhome\u002Fflyerhzm\u002FDownloads\u002Ftest_bullet\u002Fapp\u002Fviews\u002Fposts\u002Findex.html.erb:11:in `each'\n  \u002Fhome\u002Fflyerhzm\u002FDownloads\u002Ftest_bullet\u002Fapp\u002Fviews\u002Fposts\u002Findex.html.erb:11:in `_render_template__600522146_80203160_0'\n  \u002Fhome\u002Fflyerhzm\u002FDownloads\u002Ftest_bullet\u002Fapp\u002Fcontrollers\u002Fposts_controller.rb:7:in `index'\n```\n\nThe generated SQL is:\n\n```\nPost Load (1.0ms)   SELECT * FROM \"posts\"\nComment Load (0.4ms)   SELECT * FROM \"comments\" WHERE (\"comments\".post_id = 1)\nComment Load (0.3ms)   SELECT * FROM \"comments\" WHERE (\"comments\".post_id = 2)\n```\n\n9\\. To fix the N+1 query, change `app\u002Fcontrollers\u002Fposts_controller.rb` file\n\n```ruby\ndef index\n  @posts = Post.includes(:comments)\n\n  respond_to do |format|\n    format.html # index.html.erb\n    format.xml  { render :xml => @posts }\n  end\nend\n```\n\n10\\. Refresh `http:\u002F\u002Flocalhost:3000\u002Fposts`. Now there's no alert box and nothing new in the log.\n\nThe generated SQL is:\n\n```\nPost Load (0.5ms)   SELECT * FROM \"posts\"\nComment Load (0.5ms)   SELECT \"comments\".* FROM \"comments\" WHERE (\"comments\".post_id IN (1,2))\n```\n\nN+1 query fixed. Cool!\n\n11\\. Now simulate unused eager loading. Change\n`app\u002Fcontrollers\u002Fposts_controller.rb` and\n`app\u002Fviews\u002Fposts\u002Findex.html.erb`\n\n```ruby\ndef index\n  @posts = Post.includes(:comments)\n\n  respond_to do |format|\n    format.html # index.html.erb\n    format.xml  { render :xml => @posts }\n  end\nend\n```\n\n```\n\u003C% @posts.each do |post| %>\n  \u003Ctr>\n    \u003Ctd>\u003C%= post.name %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Show', post %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Edit', edit_post_path(post) %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>\u003C\u002Ftd>\n  \u003C\u002Ftr>\n\u003C% end %>\n```\n\n12\\. Refresh `http:\u002F\u002Flocalhost:3000\u002Fposts`, and you will see a popup alert box that says\n\n```\nThe request has unused preload associations as follows:\nmodel: Post => associations: [comment]\nThe request has N+1 queries as follows:\nNone\n```\n\nMeanwhile, there's a line appended to `log\u002Fbullet.log`\n\n```\n2009-08-25 21:13:22[INFO] Unused preload associations: PATH_INFO: \u002Fposts;    model: Post => associations: [comments]·\nRemove from your finder: :include => [:comments]\n```\n\n13\\. Simulate counter_cache. Change `app\u002Fcontrollers\u002Fposts_controller.rb`\nand `app\u002Fviews\u002Fposts\u002Findex.html.erb`\n\n```ruby\ndef index\n  @posts = Post.all\n\n  respond_to do |format|\n    format.html # index.html.erb\n    format.xml  { render :xml => @posts }\n  end\nend\n```\n\n```\n\u003C% @posts.each do |post| %>\n  \u003Ctr>\n    \u003Ctd>\u003C%= post.name %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= post.comments.size %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Show', post %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Edit', edit_post_path(post) %>\u003C\u002Ftd>\n    \u003Ctd>\u003C%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>\u003C\u002Ftd>\n  \u003C\u002Ftr>\n\u003C% end %>\n```\n\n14\\. Refresh `http:\u002F\u002Flocalhost:3000\u002Fposts`, then you will see a popup alert box that says\n\n```\nNeed counter cache\n  Post => [:comments]\n```\n\nMeanwhile, there's a line appended to `log\u002Fbullet.log`\n\n```\n2009-09-11 10:07:10[INFO] Need Counter Cache\n  Post => [:comments]\n```\n\nCopyright (c) 2009 - 2022 Richard Huang (flyerhzm@gmail.com), released under the MIT license\n","Bullet 是一个 Ruby 宝石，旨在通过减少应用程序中的查询次数来提升性能。它能够监测 N+1 查询问题，并提醒开发者添加必要的预加载以优化数据库访问效率；同时也会指出不必要的预加载和推荐使用计数缓存的情况。该项目支持 ActiveRecord 4.0 及以上版本以及 Mongoid 4.0 及以上版本。适合在开发阶段或自定义环境（如测试、性能分析等）中启用，帮助开发者识别并解决潜在的性能瓶颈，但不建议在生产环境中开启以免影响用户体验。","2026-06-11 03:13:42","top_language"]