# HABTM checkboxes insert but don't delete

**URL:** <https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234>\
**Category:** Rails\
**Created:** [December 9, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234 "2008-12-09T19:06:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tecregio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6f9a4e/32.png) [@tecregio](https://www.ruby-forum.com/u/tecregio)\
**Post date:** [December 9, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234/1 "2008-12-09T19:06:47Z")

</div>

I have a problem related with the HABTM relationships with checkboxes.

I’ve got a profile and a service model, both related with  
has\_and\_belongs\_to\_many.

The problem is that i can insert new data in the profiles\_services  
table, however, i cant delete already existing data.

What could be the problem?

---

<div class="post-metadata">

**Author:** ![tecregio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6f9a4e/32.png) [@tecregio](https://www.ruby-forum.com/u/tecregio)\
**Post date:** [December 9, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234/2 "2008-12-09T19:15:01Z")

</div>

have you watched…

“[#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17-habtm-checkboxes)”

---

<div class="post-metadata">

**Author:** ![tecregio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6f9a4e/32.png) [@tecregio](https://www.ruby-forum.com/u/tecregio)\
**Post date:** [December 9, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234/3 "2008-12-09T19:22:37Z")

</div>

On Dec 9, 12:10 pm, tecregio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a problem related with the HABTM relationships with checkboxes.
> 
> I’ve got a profile and a service model, both related with  
> has\_and\_belongs\_to\_many.
> 
> The problem is that i can insert new data in the profiles\_services  
> table, however, i cant delete already existing data.
> 
> What could be the problem?

Try putting this ProfilesController#update , before #update\_attributes  
is called:  
params[:profile][:service\_ids] ||= []

And put this in ServicesController#update , before #update\_attributes  
is called:  
params[:service][:profile\_ids] || = []

These are needed because forms don’t submit values for unticked  
checkboxes. If you watch the “HABTM Checkboxes” RailsCast, you’ll  
learn more about this:

> **[\#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17-habtm-checkboxes)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![tecregio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6f9a4e/32.png) [@tecregio](https://www.ruby-forum.com/u/tecregio)\
**Post date:** [December 10, 2008, 12:13am UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234/4 "2008-12-10T00:13:02Z")

</div>

hehe silly me, i should have shown you the entire code, i had a  
service= definition that was conflicting with the deletion =p

---

<div class="post-metadata">

**Author:** ![tecregio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6f9a4e/32.png) [@tecregio](https://www.ruby-forum.com/u/tecregio)\
**Post date:** [December 9, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/habtm-checkboxes-insert-but-dont-delete/155234/5 "2008-12-09T19:26:08Z")

</div>

yes, i think i’m following exactly the tutorial, however it doesn’t  
work…

here’s the code:

#profile model:

class Profile \< ActiveRecord::Base  
belongs\_to :company  
has\_and\_belongs\_to\_many :services  
[…]  
end

# 

#service model:

class Service \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :profiles

validates\_presence\_of :name

end

# 

#profile\_controller

def update  
params[:profile][:service\_ids] ||= []  
@profile = Profile.find(params[:id])  
@total\_services = Service.find(:all, :order =\> ‘name’)  
@profile.update\_attributes(params[:profile])  
@company = @profile.company  
redirect\_to :action =\> ‘show’, :id =\> @company.id  
end

# 

#partial that displays the services (i’m calling @total\_services from  
the show controller)

\<% for s in @total\_services -%\>

\<% end %\>

| | 

\<%= check\_box\_tag “profile[service\_ids][]”, s.id,  
@profile.services.include?(s) %\>

\<%= s.name -%\>

 |

# 

# 

I’ve checked the generated log, and it creates the insert query when i  
have newly checked services in my view, but it doesn’t create the  
delete query when i do uncheck.
