Skip to content
a code metric tool for rails projects
Branch: master
Clone or download
Latest commit c1a4c1e Oct 29, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets Update Gemfile and refactor erb tempate Aug 17, 2014
spec Auto corrected by following Ruby Layout/TrailingWhitespace Jul 22, 2018
.travis.yml remove rubocop Jul 21, 2017 Bumping version to 1.19.4 Oct 29, 2018
Gemfile remove mac only gems Jul 24, 2018
MIT_LICENSE rails_best_practices is moved to railsbp organization now Dec 28, 2011
Rakefile Auto corrected by following Style/ExpandPathArguments May 31, 2018
rails_best_practices.gemspec Auto corrected by following Ruby Layout/TrailingWhitespace Jul 22, 2018
rails_best_practices.yml Added to config file Nov 18, 2015


Gem Version Build Status Coverage Status AwesomeCode Status for flyerhzm/rails_best_practices

Coderwall Endorse Click here to lend your support to: rails best practices and make a donation at !

rails_best_practices is a code metric tool to check the quality of Rails code.

It supports the following ORM/ODMs:

  • activerecord
  • mongoid
  • mongomapper

And the following template engines:

  • erb
  • haml
  • slim
  • rabl

rails_best_practices supports Ruby 1.9.3 or newer.

External Introduction

Ruby5 - Episode #253

Railscasts - #252 Metrics Metrics Metrics


At the root directory of a Rails app, run:

rails_best_practices .

Or for HTML output:

rails_best_practices -f html .

By default rails_best_practices will parse code in the vendor, spec, test and features directories.

Excluding directories

To exclude a directory simply call it with -e or --exclude:

rails_best_practices -e "db/migrate" .

To exclude multiple directories, separate them with comma:

rails_best_practices -e "db/migrate,vendor" .

Other command-line options

To see the full list of command-line options, run:

$ rails_best_practices -h

Usage: rails_best_practices [options]
    -d, --debug                      Debug mode
    -f, --format FORMAT              output format
        --without-color              only output plain text without color
        --with-textmate              open file by textmate in html format
        --with-sublime               open file by sublime in html format (requires
        --with-mvim                  open file by mvim in html format
        --with-github GITHUB_NAME    open file on github in html format. GITHUB_NAME is like railsbp/rails-bestpractices OR full URL to GitHub:FI repo
        --with-hg                    display hg commit and username, only support html format
        --with-git                   display git commit and username, only support html format
        --template TEMPLATE          customize erb template
        --output-file OUTPUT_FILE    output html file for the analyzing result
        --silent                     silent mode
        --vendor                     include vendor files
        --spec                       include spec files
        --test                       include test files
        --features                   include features files
    -x, --exclude PATTERNS           Don't analyze files matching a pattern
                                     (comma-separated regexp list)
    -o, --only PATTERNS              analyze files only matching a pattern
                                     (comma-separated regexp list)
    -g, --generate                   Generate configuration yaml
    -c, --config CONFIG_PATH         configuration file location (defaults to config/rails_best_practices.yml)
    -v, --version                    Show this version
    -h, --help                       Show this message




Team Blog

Google Group:


Issue Tracker:


gem install rails_best_practices

or add it to the Gemfile

gem "rails_best_practices"



Editor Integration

TextMate 2

If you use TextMate 2, you can install the RailsBestPractices.tmbundle bundle.


If you install the rails_best_practices with bundler-installed GitHub-sourced gem, please use the following command instead.

bundle exec rails_best_practices .

If you encounter a NoMethodError exception, or a syntax error, you can use debug mode to discover which file is to blame:

rails_best_practices -d .

That will provide the error's stack trace and the source code of the file which is causing the error.

Custom Configuration

First run:

rails_best_practices -g

to generate rails_best_practices.yml file.

Now you can customize this configuration file. The default configuration is as follows:

AddModelVirtualAttributeCheck: { }
AlwaysAddDbIndexCheck: { }
#CheckSaveReturnValueCheck: { }
#CheckDestroyReturnValueCheck: { }
DefaultScopeIsEvilCheck: { }
DryBundlerInCapistranoCheck: { }
#HashSyntaxCheck: { }
IsolateSeedDataCheck: { }
KeepFindersOnTheirOwnModelCheck: { }
LawOfDemeterCheck: { }
#LongLineCheck: { max_line_length: 80 }
MoveCodeIntoControllerCheck: { }
MoveCodeIntoHelperCheck: { array_count: 3 }
MoveCodeIntoModelCheck: { use_count: 2 }
MoveFinderToNamedScopeCheck: { }
MoveModelLogicIntoModelCheck: { use_count: 4 }
NeedlessDeepNestingCheck: { nested_count: 2 }
NotRescueExceptionCheck: { }
NotUseDefaultRouteCheck: { }
NotUseTimeAgoInWordsCheck: { }
OveruseRouteCustomizationsCheck: { customize_count: 3 }
ProtectMassAssignmentCheck: { }
RemoveEmptyHelpersCheck: { }
#RemoveTabCheck: { }
RemoveTrailingWhitespaceCheck: { }
RemoveUnusedMethodsInControllersCheck: { except_methods: [] }
RemoveUnusedMethodsInHelpersCheck: { except_methods: [] }
RemoveUnusedMethodsInModelsCheck: { except_methods: [] }
ReplaceComplexCreationWithFactoryMethodCheck: { attribute_assignment_count: 2 }
ReplaceInstanceVariableWithLocalVariableCheck: { }
RestrictAutoGeneratedRoutesCheck: { }
SimplifyRenderInControllersCheck: { }
SimplifyRenderInViewsCheck: { }
#UseBeforeFilterCheck: { customize_count: 2 }
UseModelAssociationCheck: { }
UseMultipartAlternativeAsContentTypeOfEmailCheck: { }
UseObserverCheck: { }
#UseParenthesesInMethodDefCheck: { }
UseQueryAttributeCheck: { }
UseSayWithTimeInMigrationsCheck: { }
UseScopeAccessCheck: { }
UseTurboSprocketsRails3Check: { }

Now, at the root directory of a Rails app, run:

rails_best_practices . -c config/rails_best_practices.yml

You can remove or comment a review to disable it, and you can change the options.

You can apply the ignored_files option on any rule by giving a regexp or array of regexps describing the path of the files you don't want to be checked:

DefaultScopeIsEvilCheck: { ignored_files: 'user\.rb' }
LongLineCheck: { max_line_length: 80, ignored_files: ['db/migrate', 'config/initializers'] }


Move code from Controller to Model

  1. Move finder to named_scope (rails2 only)
  2. Use model association
  3. Use scope access
  4. Add model virtual attribute
  5. Replace complex creation with factory method
  6. Move model logic into the Model
  7. Check return value of "save!"

RESTful Conventions

  1. Overuse route customizations
  2. Needless deep nesting
  3. Not use default route
  4. Restrict auto-generated routes


  1. Keep finders on their own model (rails2 only)
  2. The law of demeter
  3. Use observer
  4. Use query attribute
  5. Remove unused methods in models
  6. Protect mass assignment
  7. Destroy return value (disabled by default)


  1. Use multipart/alternative as content_type of email


  1. Isolating seed data
  2. Always add database index
  3. Use say with time in migrations


  1. Use before_filter (disabled by default)
  2. Simplify render in controllers
  3. Remove unused methods in controllers


  1. Remove empty helpers
  2. Remove unused methods in helpers


  1. Move code into controller
  2. Move code into model
  3. Move code into helper
  4. Replace instance variable with local variable
  5. Simplify render in views
  6. Not use time_ago_in_words


  1. Dry bundler in Capistrano
  2. Speed up assets precompilation with turbo-sprockets-rails3


  1. Remove trailing whitespace
  2. Remove tab (disabled by default)
  3. Hash syntax (disabled by default)
  4. Use parentheses in method definition (disabled by default)
  5. Long line (disabled by default)
  6. Not rescue exception

Write Your Own Checklist

If you want to write your own checklist (some checklist only for your Rails projects), please read this first, How to write your own check list?


If you want to add your rails best practices into the gem, please post your best practices on

Contact Us

We provide Rails consulting services, you can contact us by Twitter or email.

Follow us on twitter:

Send us email:

Copyright © 2009 - 2013 Richard Huang (, released under the MIT license

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.