I’m developing a form that I would like the user to have the option to return to. Ultimately all the fields need to be completed and I would like to incorporate the proper model level field validation before labeling the record as “complete”. I can think of a few ways to do this:
-
Create two tables, one for records in “draft” state, with looser validation rules (ie fields don’t necessarily need to be complete for the record to be saved), and a second table to store the records that have been submitted as “complete”, obviously with the more stringent validation rules.
-
Create only one table to store the records, with a field that is labeled “isComplete”, and based on this value determine which validation rules to apply.
I’m leaning towards option #2 because it would involve less working parts (in option #1 I would have to ensure that when I change a records state from “Draft” to “Complete” it gets deleted from one table and added to the other). The issue is I don’t know how how do this elegantly in Rails.
Ultimately, as I’m sure this problem has been solved before, my question is:
What is the best practice in this situation?
Definitely option number two. Have an isComplete or draft option that is a boolean. Then in your models you can control what validations to run based on the state of the isComplete field. This can be done in a number of ways, for instance Rails has the concept of conditional validation which allows you to specify :if options on the validations so you can restrict what runs based on the complete state. You can also add a before_save or before_update hook to run methods based on if the record is a draft or not. With these two tools you should be able to have everything in a single table in an intuitive way.