Trying to figure out the best way of clearing nested models if its parent has a field of a particular value.
I have models Company
, Person
and Role
. A company has_many people
and a person can be a contractor, which is determined by a boolean value set in the people
table, set by the following radio button:
<%= f.input :contractor, as: :radio_buttons, label: 'Is this person a contractor?', input_html: { class: 'entity_tf form-control rtf radio radio-false' } %>
Now in the view if the user clicks "Yes", the user is presented a button with which they can add dynamic cocoon fields, listing the roles that this person-contractor has.
My question is:
Suppose after adding a bunch of roles, a user suddenly realises the person he was editing wasn't actually a contractor (or something like that) and decided to change the radio button to "No." The way I've set this up, the radio fields would be simply be hidden. Meaning that they would be saved upon the form being submitted. How can I best delete them once the form is submitted if the radio button is set to false? I'm thinking of adding some conditional/validation to the model.
thanks for any ideas.