# Has\_and\_belongs\_to\_many - help?

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136>\
**Category:** Rails\
**Created:** [July 15, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136 "2009-07-15T12:12:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 15, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/1 "2009-07-15T12:12:36Z")

</div>

I know it has been asked many times, but i could not find any working  
examples even after 2 days for googling. So, i thought let me ask here.  
How do I save in HABTM

Its a simple application where users should be able to post recipes and  
that recipe HABTM categories. Multiple categories are selected through  
multi select list box.

1. I have 2 models: Recipe and Category
2. I have one join table: categories\_recipes (category\_id, recipe\_id)

Now this is in my controller:

class RecipesController \< ApplicationController  
def new  
@recipe = Recipe.new  
@categories = Category.find(:all)  
end

def create  
@recipe = Recipe.new(params[:recipe])  
@recipe.category\_recipe\_ids[]\<\<(params[:category\_recipe\_ids])  
@recipe.save  
redirect\_to :action=\>‘show’, :id=\>@recipe.id  
end

def show  
@recipe=Recipe.find(params[:id])  
end  
end

My new.html.erb

\<% form\_for :recipe, @recipe, :url =\> { :action =\> “create” } do |f| %\>  
\<%= f.text\_field :title, :maxlength =\> 100 %\>  
  
\<%= f.text\_area :description, :rows=\>‘xx’, :cols=\>‘yy’ %\>  
  
\<%= select\_tag ‘category\_recipe\_ids[]’,  
options\_from\_collection\_for\_select(@categories, :id, :title), {  
:multiple =\> true, :size =\>5, :id =\> “categories\_recipes\_id” } %\>

  
\<%= submit\_tag ‘save’,  
:style=\>‘font-size:14px;font-weight:bold;width:100px’ %\>  
  
\<% end %\>

Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 15, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/2 "2009-07-15T21:31:14Z")

</div>

I’m new to rails and will be interested how this works out, so I’ll be  
watching this thread.

What errors are you getting?

On Wed, Jul 15, 2009 at 6:12 AM, Rails L. \<  
removed\_email\_address@domain.invalid\> wrote:

> 1. I have one join table: categories\_recipes (category\_id, recipe\_id)  
> @recipe = Recipe.new(params[:recipe])  
> My new.html.erb
> 
> ## Any help is much appreciated.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Rick R

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 15, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/3 "2009-07-15T21:34:45Z")

</div>

Could you also paste your models and migrations?

On Wed, Jul 15, 2009 at 3:30 PM, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ## I know it has been asked many times, but i could not find any working Now this is in my controller: @recipe.save \<% form\_for :recipe, @recipe, :url =\> { :action =\> “create” } do |f| %\>
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > > >
> 
> –  
> Rick R

–  
Rick R

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 15, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/4 "2009-07-15T21:58:42Z")

</div>

Have you looked this tutorial  
[http://www.buildingwebapps.com/podcasts/6789-resources-page-links-categories-and-habtm/show\_notes](http://www.buildingwebapps.com/podcasts/6789-resources-page-links-categories-and-habtm/show_notes)(download  
the second link source code.)

Again, I’m new, so take this for what its worth…

Notice in their create method for pages and links (the two that have the  
habtm relationship) there is no need to add the collection to the main  
object. It looks like you shouldn’t need:

@recipe.category\_recipe\_ids[]\<\<(params[:category\_recipe\_ids])

You should just be able to use

@recipe = Recipe.new

And it should pull in the selected categories from your select options.

Not sure if it matters but in the example posted they are using a  
different  
select tag than you’re using:

\<%= f.collection\_select :category\_ids, Category.find(:all, :order =\>  
‘title’), :id, :title, {}, :multiple =\> true %\>

Actually looking at your select tag I’m not sure how something like  
“recipe\_ids” would be set the way you have it:

\<%= select\_tag ‘category\_recipe\_ids[]’,  
options\_from\_collection\_for\_select(@categories, :id, :title), {  
:multiple =\> true, :size =\>5, :id =\> “categories\_recipes\_id” }  
%\>  
Why not try something like have shown:

\<%= f.collection\_select :category\_ids, Recipe.find(:all), :id, :title,  
{},  
:multiple =\> true %\>

Also if you were going to be setting the categories in recipes (which I  
don’t think you need to do anyway), wouldn’t it be something like

@recipe.category\_ids \<\<(params[:category\_ids])

I don’t think you’d set the category\_recipe\_id ? (Again, I’m new so I  
could  
be way off here.)

On Wed, Jul 15, 2009 at 6:12 AM, Rails L. \<  
removed\_email\_address@domain.invalid\> wrote:

> 1. I have one join table: categories\_recipes (category\_id, recipe\_id)  
> @recipe = Recipe.new(params[:recipe])  
> My new.html.erb
> 
> ## Any help is much appreciated.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Rick R

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 16, 2009, 8:51am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/5 "2009-07-16T08:51:07Z")

</div>

Forgot to include the joining table which is

categories\_recipes(category\_id, recipe\_id)

You will have to create this table manually without id column.

---

<div class="post-metadata">

**Author:** ![railslist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/65b543/32.png) [@railslist](https://www.ruby-forum.com/u/railslist)\
**Post date:** [July 16, 2009, 8:14am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-help/172136/6 "2009-07-16T08:14:37Z")

</div>

Riiiick, you are a star. Atlast we have a working example for HABTM. I  
have given here the complete code if anyone needs it.

Problem: A recipe can have many categories and a Category can have many  
recipes.

Model: Recipe

class Recipe \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end

Model: Category

class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :recipes  
end

Controller: recipes\_controller

class RecipesController \< ApplicationController  
def new  
@recipe = Recipe.new  
@categories = Category.find(:all)  
end

def create  
@recipe = Recipe.new(params[:recipe])  
@recipe.save  
redirect\_to :action=\>‘show’, :id=\>@recipe.id  
end

def show  
@recipe=Recipe.find(params[:id])  
end  
end

View: New.html.erb

\<% form\_for :recipe, @recipe, :url =\> { :action =\> “create” } do |f| %\>  
\<%= f.text\_field :title, :maxlength =\> 100 %\>  
\<%= f.collection\_select :category\_ids, @categories, :id, :title, {},  
:multiple =\> true %\>  
\<%= submit\_tag ‘save’,  
:style=\>‘font-size:14px;font-weight:bold;width:100px’ %\>  
  
\<% end %\>

View: Show.html.erb

\<%= @recipe.title %\>  
\<% @recipe.categories.each do |c|%\>  
\<%= c.title %\>  
\<% end %\>
