I have a model where there are two fields that can technically be null. The field names are :is_activated and :activated_at. :activated_at is only required if :is_activated is set to true. It does not need to be present if :is_activated is false. What’s the appropriate way in Rails to set this validation directly into ActiveRecord?
I have a model where there are two fields that can technically be null.
Share
You can use a Proc on the
:activated_atvalidator.Recommended Reading:
Finally, you should consider renaming
:is_activatedto simply:activated. This is considered better practice in Rails. Theis_prefix is used in other languages for boolean attributes because their method names don’t support a?character. Ruby does, and Rails generates___?methods on boolean attributes. Because of this it’s better to just call the attribute:activatedand check it withactivated?.