# Is there an equivalent delete method for association.build?

**URL:** <https://www.ruby-forum.com/t/is-there-an-equivalent-delete-method-for-association-build/141346>\
**Category:** Rails\
**Created:** [June 24, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/is-there-an-equivalent-delete-method-for-association-build/141346 "2008-06-24T21:58:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/67e7ee/32.png) [@kap](https://www.ruby-forum.com/u/kap)\
**Post date:** [June 24, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/is-there-an-equivalent-delete-method-for-association-build/141346/1 "2008-06-24T21:58:54Z")

</div>

Hi,

I prefer to build the new state in an instance before committing  
(after validate passes). For example, I have groups and users, and use  
memberships to associate them:

class Membership \< AR::Base  
belongs\_to :user  
belongs\_to :group  
end

class Group \< AR::Base  
has\_many :memberships  
has\_many :users, :through =\> :memberships  
end

class User \< AR::Base  
has\_many :memberships  
has\_many :groups, :through =\> :memberships  
end

I add users to groups in the groups controller, and send an array of  
user id’s to the groups controller like so: group[:users][] - then in  
the controller:

def update  
group = Group.find(params[:id])  
group.update\_attributes(params[:group])  
group.save  
end

And then in Group:

def users=(args)  
memberships.clear  
User.find(args).each do |user|  
memberships.build(:user =\> user, :group =\> group)  
end  
end

First question: The problem is, that memberships.clear hits the DB  
immediatly. I would rather clear the association in memory and then do  
the delete/inserts upon commit. Is there an operation equivalent to  
build which just removes a membership from the association - but only  
in memory?

Second question: Using the users=(args) approach is convenient, but  
somehow not quite right because the method could rely on other  
properties of the group instance, eg. is\_open\_for\_new\_users? - I’m  
thinking of adding a set\_users method instead, and then in the  
controller:

group.update\_attributes(params[:group])  
group.set\_users(params[:users])  
group.save

Any thoughts on the pros/cons of these approaches? I’m not quite  
certain where to put the transaction when updating multiple model  
types, but I guess that placing it in the controller, wrapping all  
operations on group in the transaction is a way.

Any insight much appreciated.
