# HABTM & collection\_ids: confirm changes before saving

**URL:** <https://www.ruby-forum.com/t/habtm-collection-ids-confirm-changes-before-saving/180928>\
**Category:** Rails\
**Created:** [December 31, 2009, 8:10pm UTC](https://www.ruby-forum.com/t/habtm-collection-ids-confirm-changes-before-saving/180928 "2009-12-31T20:10:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 31, 2009, 8:10pm UTC](https://www.ruby-forum.com/t/habtm-collection-ids-confirm-changes-before-saving/180928/1 "2009-12-31T20:10:16Z")

</div>

It seems that when a HABTM (or has many) is involved, having a  
confirmation page prior to saving doesn’t work as seamlessly as other  
Rails view/model parings, specifically when confirming an edit. This  
is caused by the immediate inserts or deletes that take place upon  
assigning to collection\_ids. Changes are saved regardless of whether  
the user wants to cancel or proceed.

select\_tag ‘product[provider\_ids][]’, collection\_options\_with\_selected  
#pseudo

def preview  
@product = Product.find params[:id]  
@product.attributes = params[:product] #provider\_ids saved here

if @product.valid?  
render 'confirm;  
else  
render ‘edit’  
end  
end

build is not appropriate and push has the same functionality as  
assigning to provider\_ids.  
So I have to do this:

def preview  
@product = Product.find params[:id]  
@product.attributes = params[:product] #no product

@selected\_providers = Provider.find\_by\_ids params[:provider\_ids]  
#to display provider names in confirm page  
#…  
end

def update  
@product = Product.find params[:id]  
@product.attributes = params[:product]  
@provider\_ids = params[:provider\_ids]

if @product.valid?  
Product.transaction do  
@product.provider\_ids = @provider\_ids  
@product.save  
end  
else …  
end

There’s no way to do this in the typical Rails attribute assignment  
fashion?

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![MaggotChild](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@MaggotChild](https://www.ruby-forum.com/u/MaggotChild)\
**Post date:** [December 31, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/habtm-collection-ids-confirm-changes-before-saving/180928/2 "2009-12-31T20:11:26Z")

</div>

On Dec 31, 11:08 am, MaggotChild [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def preview  
> @product = Product.find params[:id]  
> @product.attributes = params[:product] #no product

I meant that no provider\_ids are included in params

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
