# adding HABTM association while creating a record

**URL:** <https://discuss.rubyonrails.org/t/adding-habtm-association-while-creating-a-record/58463>\
**Category:** rubyonrails-talk\
**Created:** [April 16, 2011, 10:55am UTC](https://discuss.rubyonrails.org/t/adding-habtm-association-while-creating-a-record/58463 "2011-04-16T10:55:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sachin\_Joshi](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sachin\_Joshi](https://discuss.rubyonrails.org/u/Sachin_Joshi)\
**Post date:** [April 16, 2011, 10:55am UTC](https://discuss.rubyonrails.org/t/adding-habtm-association-while-creating-a-record/58463/1 "2011-04-16T10:55:13Z")

</div>

Hello Experts, I have a HABTM association as followed:

class Account \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :user end

class User \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :type &nbsp;&nbsp;has\_and\_belongs\_to\_many :account end

class Type \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :user end

I have the following query; I Have a parent user who is creating another user under its account. How can i add association record between the new user (that is getting added) and the parent user's account. my form is as followed:

&nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :username %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :username %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :email %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_field :email %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :password %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.password\_field :password %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<div class="field"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label :password\_confirmation %\>\<br /\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.password\_field :password\_confirmation %\> &nbsp;&nbsp;\</div\>

&nbsp;&nbsp;&nbsp;\<% for type in Type.find(:all) %\> &nbsp;&nbsp;&nbsp;&nbsp;\<div\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box\_tag "user[type\_ids]", type.id, @user.type.include?(type) %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= type.type\_name %\> &nbsp;&nbsp;&nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<% end %\>

&nbsp;&nbsp;\<div class="actions"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.submit %\> &nbsp;&nbsp;\</div\>

Thanks in advance for any guidelines.
