Skip to content

Files

Latest commit

 

History

History
33 lines (22 loc) · 845 Bytes

Rails-RedundantPresenceValidationOnBelongsTo.md

File metadata and controls

33 lines (22 loc) · 845 Bytes

Pattern: Redundant presence validator for belongs_to

Issue: -

Description

Since Rails 5.0 the default for belongs_to is optional: false unless config.active_record.belongs_to_required_by_default is explicitly set to false. The presence validator is added automatically, and explicit presence validation is redundant.

Examples

# bad
belongs_to :user
validates :user, presence: true

# bad
belongs_to :user
validates :user_id, presence: true

# bad
belongs_to :author, foreign_key: :user_id
validates :user_id, presence: true

# good
belongs_to :user

# good
belongs_to :author, foreign_key: :user_id

Further Reading