Rails best practices
- Check the return value of "save", otherwise use "save!" The same goes for create & update.
- Never use
has_many :through. The first one has unexpected hidden behaviors and, if you find out that you need an extra column in the intermediate table, converting it to the second macro requires a fair amount of work.
- Never rescue the Exception class
ENV.fetchfor environment variables instead of
ENVso that unset environment variables are detected on deploy.
- Avoid bypassing validations with methods like
- Don't change a migration after it has been merged into master if the desired change can be solved with another migration.
- 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.
- If there are default values, set them in migrations.
- Validate the associated
user), not the database column (
- Use private instead of protected when defining controller methods.
- Name date columns with
- Name datetime columns with
- Name time columns (referring to a time of day with no date) with
- Name initializers for their gem name.
- Order ActiveRecord associations above ActiveRecord validations.
- Order the controller contents: filters, public methods, private methods.
- Order i18n translations alphabetically by key name.
- Put application-wide partials in the [
- Use the default
render 'partial'syntax over
render partial: 'partial'.
- Avoid the
:exceptoption in routes. Use the
:onlyoption to explicitly state exposed routes.
- Use the user's name in the
Fromheader and email in the
Reply-Towhen delivering an email on behalf of the app's users
- Don't use before_actions for setting instance variables. Use them only for changing the application flow, such as redirecting if a user is not authenticated.