How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Rails · Ruby

Fix ArgumentError: 'invalid_status' is not a valid status in Rails

This error occurs when you assign a value to an enum attribute that is not in the defined list of valid values. Rails enums only accept declared values and raise an ArgumentError for anything else. Validate the input before assignment or rescue the error and return a meaningful message to the user.

Reading the Stack Trace

ArgumentError ('invalid_status' is not a valid status): activerecord (7.1.3) lib/active_record/enum.rb:185:in `assert_valid_value' activerecord (7.1.3) lib/active_record/enum.rb:155:in `cast' activemodel (7.1.3) lib/active_model/attribute.rb:58:in `value' app/controllers/orders_controller.rb:18:in `update' actionpack (7.1.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'

Here's what each line means:

Common Causes

1. User input not validated

The controller directly assigns user-provided status without checking if it is a valid enum value.

def update
  @order = Order.find(params[:id])
  @order.status = params[:status]  # Could be any string
  @order.save!
end

2. Typo in enum value

Code uses a misspelled enum value that does not match the model definition.

class Order < ApplicationRecord
  enum status: { pending: 0, processing: 1, shipped: 2, delivered: 3 }
end

@order.status = 'shiped'  # Typo: should be 'shipped'

3. Enum values changed without migration

The enum definition was updated but existing data contains old values.

# Old: enum status: { active: 0, inactive: 1 }
# New: enum status: { enabled: 0, disabled: 1 }
# Database still has 'active' and 'inactive' string values if using string column

The Fix

Check if the provided status is a valid enum value using Order.statuses.key? before assignment. Return a helpful error message listing valid values when the input is invalid.

Before (broken)
def update
  @order = Order.find(params[:id])
  @order.status = params[:status]
  @order.save!
end
After (fixed)
def update
  @order = Order.find(params[:id])
  if Order.statuses.key?(params[:status])
    @order.update!(status: params[:status])
    render json: @order
  else
    render json: { error: "Invalid status. Valid values: #{Order.statuses.keys.join(', ')}" },
           status: :unprocessable_entity
  end
end

Testing the Fix

require 'rails_helper'

RSpec.describe OrdersController, type: :request do
  let(:order) { create(:order, status: :pending) }

  describe 'PATCH /orders/:id' do
    it 'updates with a valid status' do
      patch "/orders/#{order.id}", params: { status: 'shipped' }
      expect(response).to have_http_status(:ok)
      expect(order.reload.status).to eq('shipped')
    end

    it 'returns error for invalid status' do
      patch "/orders/#{order.id}", params: { status: 'invalid_status' }
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['error']).to include('Invalid status')
    end
  end
end

Run your tests:

bundle exec rspec spec/requests/orders_spec.rb

Pushing Through CI/CD

git checkout -b fix/rails-enum-invalid,git add app/controllers/orders_controller.rb,git commit -m "fix: validate enum value before assignment",git push origin fix/rails-enum-invalid

Your CI config should look something like this:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_PASSWORD: postgres
        ports: ['5432:5432']
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.3'
          bundler-cache: true
      - run: bin/rails db:setup
      - run: bundle exec rspec

The Full Manual Process: 18 Steps

Here's every step you just went through to fix this one bug:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. Monitor production to confirm the error is resolved

Total time: 30-60 minutes. For one bug.

Or Let bugstack Fix It in Under 2 minutes

Every step above? bugstack does it automatically.

Step 1: Install the SDK

gem install bugstack

Step 2: Initialize

require 'bugstack'

Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])

Step 3: There is no step 3.

bugstack handles everything from here:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. Deploys to production (or opens a PR for review)

Time from error to fix deployed: Under 2 minutes.

Human involvement: zero.

Try bugstack Free →

No credit card. 5-minute setup. Cancel anytime.

Deploying the Fix (Manual Path)

  1. Add enum validation before assignment in all controllers.
  2. Write request specs for valid and invalid enum values.
  3. Run the full test suite.
  4. Open a pull request.
  5. Merge and verify in staging.

Frequently Asked Questions

BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.

BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.

Use integers for better storage and performance. Rails maps the integer values to symbolic names. Strings are more readable in raw SQL but take more space.

Add the new value to the enum hash in the model. If using integer backing, append it with the next number. Never reorder or remove existing integer mappings.