Validate associations within contexts but not alone

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the rails category.

Last Updated: 2024-04-24

If you insist on every record having a constellation of associations present for data validity, it is a pain in the butt to set up tests (and expensive on the DB time too)

That said, these data integrity checks might be important in production.

A good trade-off is "context validations"

validates :law_disciplines, presence: true, on: :user_interface

# Usage:
record.save(context: :user_interface)
record.valid?(:user_interace)

This allows for the best of both worlds: - convenient tests in the general case - data integrity in production