# How to make HABTM bulk-assignment transactional?

**URL:** <https://www.ruby-forum.com/t/how-to-make-habtm-bulk-assignment-transactional/142986>\
**Category:** Rails\
**Created:** [July 14, 2008, 9:07am UTC](https://www.ruby-forum.com/t/how-to-make-habtm-bulk-assignment-transactional/142986 "2008-07-14T09:07:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@nbits](https://www.ruby-forum.com/u/nbits)\
**Post date:** [July 14, 2008, 9:07am UTC](https://www.ruby-forum.com/t/how-to-make-habtm-bulk-assignment-transactional/142986/1 "2008-07-14T09:07:38Z")

</div>

I have a model with a has\_and\_belongs\_to\_many relationship that I’d  
like to bulk-assign a bunch of IDs to, as well as other attributes. Is  
there any way I can do this in a transaction without having to  
explicitly demarcate it?

Code looks something like this:

class Resource  
has\_and\_belongs\_to\_many :groups  
end

class ResourceController  
def update  
@resource = Resource.find(params[:id])  
@resource.group\_ids = params[:group\_ids].split(’,’)  
if @resource.update\_attributes(params[:resource])  
…

What I find is that as soon as I call group\_ids, the changes are  
written to the database. I want it to happen in the same transaction  
that update\_attributes uses. I’d rather not have to declare a  
transaction block, because there are already relationships between  
these objects.

Any ideas?

Thanks,

Ben
