# Help with habtm models in one form

**URL:** <https://www.ruby-forum.com/t/help-with-habtm-models-in-one-form/154874>\
**Category:** Rails\
**Created:** [December 4, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/help-with-habtm-models-in-one-form/154874 "2008-12-04T18:39:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fatih\_GenSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@Fatih\_GenSSSS](https://www.ruby-forum.com/u/Fatih_GenSSSS)\
**Post date:** [December 4, 2008, 6:39pm UTC](https://www.ruby-forum.com/t/help-with-habtm-models-in-one-form/154874/1 "2008-12-04T18:39:52Z")

</div>

Hi I have a post and a category model  
class Post \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end  
class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :posts  
end  
I created join table migration files

Now I wanna create category while creating post I mean user can choose  
category or add a new category to this post how can i imlement this?

–

---

<div class="post-metadata">

**Author:** ![Fatih\_GenSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cdc98d/32.png) [@Fatih\_GenSSSS](https://www.ruby-forum.com/u/Fatih_GenSSSS)\
**Post date:** [December 4, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/help-with-habtm-models-in-one-form/154874/2 "2008-12-04T19:03:59Z")

</div>

I did something like this in the past in a similar situation.

I have a drop down of categories that has all the valid categories and  
an additional category injected as ‘New Category’. When you user selects  
the ‘New Category’ from the drop down, I display another textbox (I did  
this with ajax call) for the user to provide new category name. In the  
controller if this new\_category param is detected than a new category is  
created first before creating the post associated with that new  
category.

-Navjeet

Fatih GenÃ§ wrote:

> Hi I have a post and a category model  
> class Post \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :categories  
> end  
> class Category \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :posts  
> end  
> I created join table migration files
> 
> Now I wanna create category while creating post I mean user can choose  
> category or add a new category to this post how can i imlement this?
> 
> –
