# Remove habtm item?

**URL:** <https://www.ruby-forum.com/t/remove-habtm-item/90808>\
**Category:** Rails\
**Created:** [April 1, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/remove-habtm-item/90808 "2007-04-01T23:17:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 1, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/remove-habtm-item/90808/1 "2007-04-01T23:17:11Z")

</div>

I’m making a website for a book club, and I’m trying to arrange it so  
that members can indicate their intention to attend the discussion of a  
given book. I’ve got that working, but I’m not able to remove these  
“votes” later on. I have

class Book \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

and adding an intention is done by:

@book.users \<\< User.find(session[:user\_id])  
if @book.save!

That works fine. However, I’m not too clear on how to _remove_ an  
intention. Right now, I’m doing this as

@book.users.delete\_if {|u| u == User.find(session[:user\_id])}  
if @book.save!

and, although the “true” part of the save is executed (it flashes), the  
book.users array is not actually changed.

Q: Is there a standard way to do this sort of thing, sort of the reverse  
of “\<\<”?

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [April 2, 2007, 2:21am UTC](https://www.ruby-forum.com/t/remove-habtm-item/90808/2 "2007-04-02T02:21:25Z")

</div>

I’m answering my own question, to help anyone who happens upon it…

The solution (gleaned from [Delete has\_many\_and\_belongs\_to\_many relation - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/74988#113245))  
is to do

@book = Book.find(params[:id])  
@user = User.find(session[:user\_id])  
@book.users.delete(@user)
