jquery - Clear all nested models if parent has field of particular value -


trying figure out best way of clearing nested models if parent has field of particular value.

i have models company, person , role. company has_many people , person can contractor, determined boolean value set in people table, set following radio button:

<%= f.input :contractor, as: :radio_buttons, label: 'is person contractor?', input_html: { class: 'entity_tf form-control rtf radio radio-false' } %> 

now in view if user clicks "yes", user presented button can add dynamic cocoon fields, listing roles person-contractor has.

my question is:

suppose after adding bunch of roles, user realises person editing wasn't contractor (or that) , decided change radio button "no." way i've set up, radio fields hidden. meaning saved upon form being submitted. how can best delete them once form submitted if radio button set false? i'm thinking of adding conditional/validation model.

thanks ideas.

use callback in person model.

after_save :remove_all_roles, unless: :is_a_contractor_bool?  def remove_all_roles     self.roles.delete(self.roles) end 

Comments