# How to remove dependents in has-many-belongs-to relationship before saving?

**URL:** <https://www.ruby-forum.com/t/how-to-remove-dependents-in-has-many-belongs-to-relationship-before-saving/186892>\
**Category:** Rails\
**Created:** [April 13, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/how-to-remove-dependents-in-has-many-belongs-to-relationship-before-saving/186892 "2010-04-13T22:13:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 13, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/how-to-remove-dependents-in-has-many-belongs-to-relationship-before-saving/186892/1 "2010-04-13T22:13:04Z")

</div>

Hi,

In my RESTful controller’s create method, how do I intercept and  
modify any dependents before saving? That is, I have a model that is  
the parent of nested associations gathered from a form. The first line  
of this model’s controller create method is:

@parent = Parent.new(params[:parent])

What if I want to tour through the nested associations and possibly  
remove a few from the association before I proceed to save the parent  
in the create method? All the discussion of removing association  
models concerns the case when models have already been persisted to a  
database, not before they’ve been saved. I’ve been fiddling around  
with this, but can’t figure out a way to ‘delete’ associations while  
they’re only in-memory.

(By way of background, I create multiple ‘rows’ representing each  
nested association in my parent model form. I allow the case that the  
user may submit the form with some of the nested association fields  
being blank. In that case, however, I’d like to sidestep any  
validations in those nested association models by removing the blank-y  
dependent model objects before the parent is saved.)

Thanks for any comments,

Grar
