- Check the return value of "save", otherwise use "save!" The same goes for create & update.
- Never use
has_and_belongs_to_many—usehas_many :throughinstead. 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
- Use
ENV.fetchfor environment variables instead ofENV[]so that the unset environment variables are detected on deploy. - Avoid bypassing validations with methods like
save(validate: false),update_attribute,update_column,update_columns, andtoggle. - 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
belongs_toobject (user), not the database column (user_id). - Use private instead of protected when defining controller methods.
- Name date columns with
_onsuffixes. - Name datetime columns with
_atsuffixes. - Name time columns (referring to a time of day with no date) with
_timesuffixes. - 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 [
app/views/application] directory. - Use the default
render 'partial'syntax overrender 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 theReply-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.