# Has\_and\_belongs\_to\_many problems when editing

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757>\
**Category:** Rails\
**Created:** [July 11, 2008, 3:49am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757 "2008-07-11T03:49:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 11, 2008, 3:49am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757/1 "2008-07-11T03:49:57Z")

</div>

Hello,

Right now I have a has\_and\_belongs\_to\_many relationship between an  
events table and a users table. So the join table rows look something  
like this:

event\_id user\_id  
1 2  
1 3  
1 5

Where those users are added through a list of checkboxes.

The problem is that if I want to edit info. and add for example one more  
user, rails will erase all existing data on the join table and replace  
it with the new user.

event\_id user\_id  
1 7

Is there a way around this?

Please help! and thanks!

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 11, 2008, 4:23am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757/2 "2008-07-11T04:23:30Z")

</div>

Could you show us the code that led to this?

On Jul 10, 6:49 pm, Elias O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 11, 2008, 5:07am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757/3 "2008-07-11T05:07:47Z")

</div>

Eric LIn wrote:

> Could you show us the code that led to this?
> 
> On Jul 10, 6:49ï¿½pm, Elias O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi,  
This is my view code for the edit action.

# Editing event

\<% form\_for ([current\_user.neighborhood, @event]) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :date %\>  
 \<%= f.datetime\_select :date %\>

\<%= f.label :title %\>  
 \<%= f.text\_field :title %\>

\<%= f.label :place %\>  
 \<%= f.text\_field :place %\>

\<%= f.label :description %\>  
 \<%= f.text\_area :description %\>

\<% # f.label :user\_id %\>  
 \<% # f.text\_field :user\_id %\> \<% # hidden\_field(:event, :user\_id, { :value =\> current\_user.id } ) %\>

\<%= f.label :event\_category\_id %\>  
 \<% # f.text\_field :event\_category\_id %\> \<%= collection\_select(:event, :event\_category\_id, @event\_categories, :id, :name) %\>

\<% unless @friends.empty? %\>

 \<% for friend in @friends do %\> - \<%= " "+friend.name %\>
 \<% end %\> 

 \<% end %\> 

\<%= f.submit "Update" %\>

\<% end %\>

\<%= link\_to ‘Show’,  
neighborhood\_event\_path(current\_user.neighborhood,@event) %\> |  
\<%= link\_to ‘Back’, neighborhood\_events\_path(current\_user.neighborhood)  
%\>

The edit and update actions:

# GET /events/1/edit

def edit  
@event = Event.find(params[:id])  
@event\_categories = EventCategory.find(:all)  
@friends = current\_user.mutual\_friends  
end

def update  
@event = Event.find(params[:id])  
respond\_to do |format|  
if @event.update\_attributes(params[:event])  
flash[:notice] = ‘Event was successfully updated.’  
format.html { redirect\_to neighborhood\_events\_path }  
format.xml { head :ok }  
else  
# Added to show again all the event\_categories on the form  
@event\_categories = EventCategory.find(:all)  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @event.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

So what’s happening is that if I edit an event and choose others friends  
(check various checkboxes on the view). This new friends will replace  
the old friends in the join table events\_users (friends are user  
objects). I want this new friends to be added to the join table without  
deleting the ones before.

Thanks again.

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 21, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-problems-when-editing/142757/4 "2008-07-21T23:43:18Z")

</div>

Just took a rough look at your code, and I believe the event[user\_ids]  
[] form elements are not used properly. I suggest you take a look at  
[#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17) for how it should be done.

Eric

On Jul 10, 8:07Â pm, Elias O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
