Rails validation and null: false Personal notes

Validation is a gatekeeper-like role that is described in advance in the model to prevent the value of a specific column sent by the requester from being left empty or being saved in an unintended manner.


In the above example,

text "Street" validates "wait" text "Eh" validates "You haven't entered anything. Start over." text "Pien"

Like this.

On the other hand, what is null: false? Actually, this is also a gatekeeper for not accepting empty data, part 2. null.png

Then, when it comes to which one is better, I write both, but the answer is correct.


The reason is that there is one decisive difference between the two factors mentioned above, which is that "the place where they are protected is different".

Validation-kun is the gatekeeper in the rails app. It will crack down on submissions from forms. On the other hand, null: false is the gatekeeper waiting in front of the database. This prevents writing directly to the database.

A relationship like a bank window and a security guard in front of a safe. As a result of the analogy, it is difficult to understand.

Without validation, the application will not behave as intended, and without null: false, it will pass through when unintended data is sent to the database in some way.

So, I want them to get along and protect the database.

Recommended Posts

Rails validation and null: false Personal notes
[Rails] Validation settings and Japanese localization
[rails] Set validation
DDD personal notes
[Rails5] Rspec -validation-
html & rails notes
Rails valid? And invalid?
Java true and false
[Rails] Customize devise validation
play framework personal notes
rails tutorial fighting notes Ⅲ