You’re not receiving notifications from this thread.
Where do I put a custom validator for a model?
I'm confused about where to put code for a custom validator I wrote, should I create a nested class inside the model file? or create a new file in models/concerns? I have the code but don't know where is the right place to put the file, can someone help me please?
Any folders in
app/ will be autoloaded, so you could create
include MyValidator in your model. Rails will autoload that and find it correctly. This is my preferred way of doing things usually.
Or you could put it in lib and require it as well.
Thank you Chris, so I guess it be ok if I put it inside the concerns folder. One more thing should it be a module or just a class definition?
Generally, the concerns folder is for modules so it might get confusing in there but it should work.
You could also probably use
include Validators::MyValidator now that I think about it.
I like this better than
My little experience is that
app/validators makes it easier to import the custom validator in the model than using
app/models/validators as with the first one there is no need to user
include my_custom_validator in the model.