Home>

I'm currently developing a web app with ruby ​​on rails, but I have a question about validation.
Is it possible to perform conditional mandatory checks in the migration file?
It seems that the model class can perform conditional mandatory checks, but when I checked the official documentation in the migration file, it seems that it is not described, so is there such a method?

In addition, both the model and the migration file can be validated such as mandatory checks, but the model is checked on the AP server and the migration file is checked on the DB server. Basically, the migration file is checked. Is it correct to recognize that the check with complicated conditions like this time is performed by the model?
I would like somebody to teach.

What I tried

Rails Guide Active Record Migration

Supplementary information (FW/tool version, etc.)

ruby 2.5.1p57
Rails 5.2.4.2

  • Answer # 1

    I wasn't familiar with it so I was watching it, but ...

    As far as I know, I've never seen a condition for a not null option in a database.

    There is a good point and another point in which one to perform the required check.

    If you do it with validation, it is easy because you can display the error message within the Rails frame.
    However, it is possible to create nul data with raw SQL.
    Also, the data created before setting validation may still be null, and I've seen a number of examples where it's bad.

    If you do it in DB, there will be no null data.
    But putting the error in the error message can be awkward.