So in my application i have:
- ModelA
- ModelB
- ModelC
- ModelD
In my controller for ModelA, i created a custom function called “is_verified”. In is_verified, i call multiple activerecord searches across ModelA/B/C/D to pull specific pieces of rows of data, do some comparison and if conditionals passes, is_verified returns ‘true’ and the function passes and ‘false’ and spits out some errors to the user
while what i am doing is simple and gets the job done, i am fairly certain its not in best practice to query multiple tables and perform conditionals to validate my function from within the controller.
what is best practice to perform cross-model validations like in my situation above.
I would move the validation to the model. I would also create multiple verification related methods on each model and just call those.
Ideally, Model A would have something like:
Then each of your models, when passed an instance of A, could see if they have any reason that that instance should not be valid.
Try to split out the validation as granular as possible, that way it’s easier to test/verify.