# Fix Ruby on Rails production errors automatically

Ruby · Rails

# Fix Ruby on Rails production errors automatically

When your Rails app raises an unhandled exception, Bugstack works out the cause and sends the fix as a pull request: a NoMethodError on nil, an ActiveRecord::RecordNotFound, a wrong number of arguments. The gem is the crash-capture part of a product that maintains your whole repository.

## What Ruby Errors Bugstack Fixes

-   **NoMethodError: undefined method for nil:NilClass** — calling methods on nil objects
-   **ActiveRecord::RecordNotFound** — querying for records that don't exist
-   **ArgumentError: wrong number of arguments** — method arity mismatches
-   **NameError: uninitialized constant** — referencing undefined classes or modules

## Install the SDK

Add the gem to your Gemfile, then run `bundle install`:

```
gem 'bugstack', '~> 1.0'
```

Then add one initializer, with your API key and the Bugstack endpoint:

```
# config/initializers/bugstack.rb
require "bugstack/integrations/rails"

Bugstack.init(
  api_key: ENV["BUGSTACK_API_KEY"],
  endpoint: ENV.fetch("BUGSTACK_ENDPOINT", "https://dashboard.bugstack.ai/api/ingest/sdk")
)

# Inside Rails' own error pages, so bugstack sees the error before they answer it:
Rails.application.config.middleware.insert_after(
  ActionDispatch::DebugExceptions,
  Bugstack::Integrations::RackMiddleware
)
```

Sinatra and plain Ruby are in the [installation guide](/docs/installation/ruby).

## Example Fix

Bugstack detects a NoMethodError caused by calling a method on nil and writes a small fix:

Before (broken)

```
def show
  @user = User.find_by(id: params[:id])
  @email = @user.email
end

# Runtime error when user is nil:
# NoMethodError: undefined method
#   'email' for nil:NilClass
```

After (fixed by Bugstack)

```
def show
  @user = User.find_by(id: params[:id])
  if @user.nil?
    head :not_found
    return
  end
  @email = @user.email
end
```

## How a Ruby fix reaches you

Bugstack’s verification sandbox, where fixes for Python, JavaScript and Go are proven before you see them, does not run Ruby yet. A Ruby fix arrives as a pull request with the cause and the change, your CI runs on it, and you review and merge it. Autopilot does not merge Ruby fixes on its own.

## What else Bugstack does for this repository

The SDK is one part. Once your repository is connected, Bugstack also scans your Ruby gems for known vulnerabilities and outdated versions, scans for secrets, and acts on failed deploys if you host on Render. It scans and reports, and it can open pull requests for what it finds. Those pull requests are switched on per account. Every change arrives as a pull request, and you set how much happens without you.

[Full installation guide with config reference →](/docs/installation/ruby)

## See it work on your own code.

$100 a month

Includes $100 of usage credit every month. Start with a 14-day trial: $50 of credit and a first security audit on us. A card is required.

[Get started](https://dashboard.bugstack.ai/login) [Book 15 minutes with the founder](https://cal.com/mason-bachmann-1atgdq/15min)