RepositoryRuby
RU

rubocop/rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.

876stars
328forks
133issues
4views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: code-formatter

#1 Repository of the month

1
TINIX TRENDING·Topic: rubocop

#1 Repository of the month

2
TINIX TRENDING·Topic: static-code-analysis

#2 Repository of the month

RuboCop Rails

Gem Version CI

A RuboCop extension focused on enforcing Rails best practices and coding conventions.

[!IMPORTANT] This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<= 1.5.0) has been renamed to rubocop-rails_config gem.

Installation

Just install the rubocop-rails gem

$ gem install rubocop-rails

or if you use bundler put this in your Gemfile

gem 'rubocop-rails', require: false

Usage

You need to tell RuboCop to load the Rails extension. There are three ways to do this:

RuboCop configuration file

Put this into your .rubocop.yml.

plugins: rubocop-rails

Alternatively, use the following array notation when specifying multiple extensions.

plugins:
  - rubocop-other-extension
  - rubocop-rails

Now you can run rubocop and it will automatically load the RuboCop Rails cops together with the standard cops.

[!NOTE] The plugin system is supported in RuboCop 1.72+. In earlier versions, use require instead of plugins.

Command line

$ rubocop --plugin rubocop-rails

Rake task

require 'rubocop/rake_task'

RuboCop::RakeTask.new do |task|
  task.plugins << 'rubocop-rails'
end

RuboCop Rails configuration

The following settings specific to RuboCop Rails can be configured in .rubocop.yml.

AllCops: TargetRailsVersion

What version of Rails is the inspected code using? If a value is specified for TargetRailsVersion then it is used. Acceptable values are specified as a float (e.g., 7.2); the patch version of Rails should not be included.

AllCops:
  TargetRailsVersion: 7.2

If TargetRailsVersion is not set, RuboCop will parse the Gemfile.lock or gems.locked file to find the version of Rails that has been bound to the application. If neither of those files exist, RuboCop will use Rails 5.0 as the default.

AllCops: MigratedSchemaVersion

By specifying the MigratedSchemaVersion option, migration files that have already been run can be ignored. When MigratedSchemaVersion: '20241225000000' is set, migration files lower than or equal to '20241225000000' will be ignored. For example, to ignore db/migrate/20241225000000_create_articles.rb and earlier migrations you would configure it the following way:

AllCops:
  MigratedSchemaVersion: '20241225000000'

This prevents inspecting schema settings for already applied migration files. Changing already applied migrations should be avoided because it can lead to the schema getting out of sync between your local copy and what it actually is in production, depending on when bin/rails db:migrate was executed. If you want to modify your schema to comply with the cops, you should instead create new migrations.

Rails configuration tip

In Rails 6.1+, add the following config.generators.after_generate setting to your config/environments/development.rb to apply RuboCop autocorrection to code generated by bin/rails g.

# config/environments/development.rb
Rails.application.configure do
  config.generators.after_generate do |files|
    parsable_files = files.filter { |file| file.end_with?('.rb') }
    unless parsable_files.empty?
      system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
    end
  end
end

It uses rubocop -A to apply Style/FrozenStringLiteralComment and other unsafe autocorrection cops. rubocop -A is unsafe autocorrection, but code generated by default is simple and less likely to be incompatible with rubocop -A. If you have problems you can replace it with rubocop -a instead.

In Rails 7.2+, it is recommended to use config.generators.apply_rubocop_autocorrect_after_generate! instead of the above setting:

 # config/environments/development.rb
 Rails.application.configure do
   (snip)
   # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
-  # config.generators.apply_rubocop_autocorrect_after_generate!
+  config.generators.apply_rubocop_autocorrect_after_generate!
 end

You only need to uncomment.

The Cops

All cops are located under lib/rubocop/cop/rails, and contain examples/documentation.

In your .rubocop.yml, you may treat the Rails cops just like any other cop. For example:

Rails/FindBy:
  Exclude:
    - lib/example.rb

Documentation

You can read a lot more about RuboCop Rails in its official docs.

Compatibility

Rails cops support the following versions:

  • Rails 4.2+

Readme Badge

If you use RuboCop Rails in your project, you can include one of these badges in your readme to let people know that your code is written following the community Rails Style Guide.

Rails Style Guide

Rails Style Guide

Here are the Markdown snippets for the two badges:

[![Rails Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop-rails)

[![Rails Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://rails.rubystyle.guide)

Contributing

Checkout the contribution guidelines.

License

rubocop-rails is MIT licensed. See the accompanying file for the full text.

Similar Projects

Discover other similar projects and AI models.

RepositoryRuby
rubocop/rubocop

A Ruby static code analyzer and formatter, based on the community Ruby style guide.

12.9k
Nov 28, 2026
RepositoryRuby
rubocop/rubocop-performance

An extension of RuboCop focused on code performance checks.

743
Nov 8, 2026
RepositoryRuby
rails/rails

Ruby on Rails

59.1k
Nov 29, 2026
RepositoryRuby
discourse/discourse

A platform for community discussion. Free, open, simple.

48.1k
Dec 1, 2026
RepositoryRuby
fastlane/fastlane

🚀 The easiest way to automate building and releasing your iOS and Android apps

42.5k
Nov 28, 2026
RepositoryRuby
gitlabhq/gitlabhq

GitLab CE Mirror | Please open new issues in our issue tracker on GitLab.com

24.6k
Oct 23, 2026
RepositoryRuby
heartcombo/devise

Flexible authentication solution for Rails with Warden.

24.3k
Oct 6, 2026
RepositoryRuby
forem/forem

For empowering community 🌱

22.8k
Nov 25, 2026
RepositoryRuby
diaspora/diaspora

A privacy-aware, distributed, open source social network.

13.7k
Nov 18, 2026