ruby
38 lines · 3 tabs
Maya Patel
Jan 2026
3 tabs
Rails.application.configure do
# Bullet configuration
config.after_initialize do
Bullet.enable = true
Bullet.alert = true # Show JavaScript alert
Bullet.bullet_logger = true # Log to bullet.log
Bullet.console = true # Show in browser console
Bullet.rails_logger = true # Add to Rails log
Bullet.add_footer = true # Add warning to HTML footer
# Raise errors in tests
Bullet.raise = true if Rails.env.test?
end
end
# BAD - N+1 query (1 query for posts + N queries for authors)
def index
@posts = Post.published.limit(20)
render json: @posts.map do |post|
{
title: post.title,
author_name: post.author.name # N queries!
}
end
end
# GOOD - Eager loading (2 queries total)
def index
@posts = Post.published
.includes(:author)
.limit(20)
render json: @posts.map do |post|
{
title: post.title,
author_name: post.author.name # No additional queries
}
end
end
3 files · ruby
Explain with highlit
N+1 queries are the most common Rails performance problem—loading associations in loops causes exponential database queries. The Bullet gem detects N+1s in development and suggests fixes. It monitors queries and alerts when you should use includes, preload, or eager_load. I configure it to show notifications in the browser console and raise errors in tests. The gem also catches unused eager loading, where you preload associations but never access them. Fixing N+1s typically involves adding includes(:association) to queries. For complex nested associations, I use strict_loading mode to catch issues early. Bullet is essential tooling—it finds issues CI can't catch.
Related snips
ruby
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
rails
hotwire
turbo
by codesnips
4 tabs
ruby
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
rails
activerecord
patterns
by Alex Kumar
1 tab
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
1 tab
ruby
class PostsController < ApplicationController
def index
@posts = Post.includes(:author)
.order(created_at: :desc)
.page(params[:page])
.per(10)
Turbo Frames: infinite scroll with lazy-loading frame
rails
turbo
hotwire
by codesnips
4 tabs
ruby
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
rails
performance
streaming
by codesnips
3 tabs
erb
<form data-controller="query-sync" data-action="change->query-sync#apply">
<select name="status" class="rounded border p-2">
<option value="">Any</option>
<option value="open">Open</option>
<option value="closed">Closed</option>
</select>
Filter UI that syncs query params via Stimulus (no front-end router)
rails
hotwire
stimulus
by Henry Kim
2 tabs
Share this code
Here's the card — post it anywhere.