- Name initializers for their gem name.
- Use
libfor code that is not app-specific and could later be extracted into a gem. - Use
app/jobsfor code that doesn't need to return anything and can be run asynchronously. - Generate necessary Spring binstubs for the project, such as
rakeandrspec, and add them to version control. - Use the
.ruby-versionfile convention to specify the Ruby version and patch level for a project. - Prefer
cookies.signedovercookiesto prevent tampering. - Use
ENV.fetchfor environment variables instead ofENV[]so that unset environment variables are detected on deploy.
- Avoid the
:exceptoption in routes. - Avoid
memberandcollectionroutes. - Order resourceful routes alphabetically by name.
- Use the
:onlyoption to explicitly state exposed routes. - Prefer resource routing over generating routes individually
- Use
_urlsuffixes for named routes in mailer views and redirects. Use_pathsuffixes for named routes everywhere else.
- Put application-wide partials in the
app/views/applicationdirectory. - Use the default
render 'partial'syntax overrender partial: 'partial'. - Use
link_tofor GET requests, andbutton_tofor other HTTP verbs. - Don't reference a model class directly from a view.
- Don't use instance variables in partials. Pass local variables to partials from view templates.
- Use only one instance variable in each view.
- Use private instead of protected when defining controller methods.
- Order controller contents: filters, public methods, private methods.
- Avoid instantiating more than one object in controllers.
Guidance on ActiveRecord, ActiveModel, and other model objects.
- Order ActiveRecord associations alphabetically by association type, then attribute name. Example.
- Order ActiveRecord validations alphabetically by attribute name.
- Order ActiveRecord associations above ActiveRecord validations.
- Order model contents: constants, macros, public methods, private methods.
- Use
def self.method, not thescope :methodDSL. #643 - Use new-style
validates :name, presence: truevalidations, and put all validations for a given column together. Example. - Avoid bypassing validations with methods like
save(validate: false),update_attribute, andtoggle. - Avoid naming methods after database columns in the same class.
- Don't return false from
ActiveModelcallbacks, but instead raise an exception. - Don't use SQL or SQL fragments (
where('inviter_id IS NOT NULL')) outside of models. - Validate the associated
belongs_toobject (user), not the database column (user_id). - Use
touch: truewhen declaringbelongs_torelationships. - Use Pundit when you need to restrict access to models and data.
- Name date columns with
_onsuffixes. - Name datetime columns with
_atsuffixes. - Back boolean concepts like deleted? or published? with timestamps columns in the database
deleted_at,published_at. This can be valuable when you need to know when something took place. Time for A Boolean provides a nice interface for this. - Name time columns (referring to a time of day with no date) with
_timesuffixes. - Keep
db/schema.rbordb/development_structure.sqlunder version control. - Use
db/seeds.rbfor data that is required in all environments. - Use
dev:primerake task for development environment seed data.
- Set an empty string as the default constraint for non-required string and text fields. Example. #159
- Set an explicit
on_deletebehavior for foreign keys. - Don't change a migration after it has been merged into
mainif the desired change can be solved with another migration. - If there are default values, set them in migrations.
- Use SQL, not
ActiveRecordmodels, in migrations. - Add foreign key constraints in migrations.
- Use blocks when declaring date and time attributes in FactoryBot factories.
- Prefer
Time.currentoverTime.now - Prefer
Date.currentoverDate.today - Prefer
Time.zone.parse("2014-07-04 16:05:37")overTime.parse("2014-07-04 16:05:37")
- Ensure that the application is setup to support multiple locales.
- Ensure that the application raises an error when a translation is missing for a given locale in development and tests.
- Order i18n translations alphabetically by key name.
- Use the user's name in the
Fromheader and email in theReply-Towhen delivering email on behalf of the app's users.
Follow the normal Code Review guidelines. When reviewing others' Rails work, look in particular for:
- Review data integrity closely, such as migrations that make irreversible changes to the data, and whether there is a related todo to make a database backup during the staging and production deploys.
- Review SQL queries for potential SQL injection.
- Review whether dependency upgrades include a reason in the commit message,
such as a link to the dependency's
ChangeLogorNEWSfile. - Review whether new database indexes are necessary if new columns or SQL queries were added.
- Review whether new scheduler (
cron) tasks have been added and whether there is a related todo in the project management system to add it during the staging and production deploys.
- Use ActiveStorage to manage file uploads that live on ActiveRecord objects.
- Don't use live storage backends like S3 or Azure in tests.