# Nested form & habtm

**URL:** <https://www.ruby-forum.com/t/nested-form-habtm/190040>\
**Category:** Rails\
**Created:** [June 11, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/nested-form-habtm/190040 "2010-06-11T22:23:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [June 11, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/nested-form-habtm/190040/1 "2010-06-11T22:23:14Z")

</div>

so i am trying to save to a join table in a habtm relationship, but i am  
having problems.

from my view, i pass in a group id with:  
= link\_to “Create New User”, new\_user\_url(:group =\> 1)

User model (user.rb)  
class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :user\_groups  
accepts\_nested\_attributes\_for :user\_groups  
end

UserGroups model (user\_groups.rb)  
class UserGroup \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

users\_controller.rb  
def new  
@user = User.new(:user\_group\_ids =\> params[:group])  
end

in the new user view, i have access to the User.user\_groups object,  
however when i submit the form, not only does it not save into my join  
table (user\_groups\_users), but the object is no longer there. all the  
other objects & attributes of my User object are persistent except for  
the user group.

i just started learning rails, so maybe i am missing something  
conceptually here, but i have been really struggling with this.
