Hi all,
We all should know the importance of validations. However, it’s so easy to forget to add validations at first and some time later we notice that it’s too late.
I’d like to propose that adding presence validation for all generated columns. For example,
rails g model Post title body deleted_at
it generates
class Post
validates :title, presence: true
validates :body, presence: true
validates :deleted_at, presence: true
end
Notice that deleted_at
can be nil
, so we need to remove that line. The point is that failing to add the validations that should exist is crucial so adding them worth removing them manually, and presence validation is the most typical case.
I know some say it’s too much, but there’s a tendency to be reluctant to change generated code, or just to forget modifying, so changing generated code could have values. After all, removing validations is easier than adding them.