# Has\_many :through

**URL:** <https://www.ruby-forum.com/t/has-many-through/237733>\
**Category:** Rails\
**Created:** [November 26, 2013, 11:13pm UTC](https://www.ruby-forum.com/t/has-many-through/237733 "2013-11-26T23:13:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 26, 2013, 11:13pm UTC](https://www.ruby-forum.com/t/has-many-through/237733/1 "2013-11-26T23:13:21Z")

</div>

Hi

I have a problem with has\_many :trough association. I have 2 tables,  
Product and Category, and when I create new Product, I should make  
checkboxes to choose in what categories the product . I’ve made model  
category\_product where I store the association from the Category and  
Product tables. Here is some of the code.

class Category \< ActiveRecord::Base

has\_many :category\_products, dependent: :destroy  
has\_many :products, :through =\> :category\_products

accepts\_nested\_attributes\_for :category\_products

class CategoryProduct \< ActiveRecord::Base

belongs\_to :category  
belongs\_to :product  
accepts\_nested\_attributes\_for :category

class Product \< ActiveRecord::Base

has\_many :category\_products, dependent: :destroy  
has\_many :categories, :through =\> :category\_products

accepts\_nested\_attributes\_for :category\_products

class ProductsController \< ApplicationController

def new  
@product = Product.new  
end

def create

```
@product = Product.new(product_params)

if @product.save

  items_array = category_params
  items_array.each do |key, value|

      if value != ""
        @category_product = CategoryProduct.new(:category => value,

```

:product =\> @product.id)  
@category\_product.save  
end

```
  #category_products.create!(:category => arrayitem.id, :product =>

```

@product.id)  
end

```
  #@categories = Category.find(category_params)

  # @categories.each do |category|
    #category_products.create!(:category => category.id, :product =>

```

@product.id)  
#end  
flash[:source] = ‘Successful’  
redirect\_to @product  
else  
render ‘new’  
end

end

def category\_params  
params.require(:product).permit({:category\_ids =\> [:id]})  
end

def product\_params  
params.require(:product).permit(:name, :price, :sale\_status,  
:quantity,  
:release\_date, {:category\_ids =\> [:id]})  
end

class CategoryProductsController \< ApplicationController

def create  
@category\_product = CategoryProduct.new(params[:product\_ids,  
:category\_ids])  
if @category\_product.save  
#sign\_in @user  
#flash[:success] = “Welcome to the Sample App!”  
#redirect\_to @user  
else  
#render ‘new’  
end  
end

end

products/new.html.erb

\<%= form\_for(@product) do |f| %\>  
\<%=f.collection\_check\_boxes :category\_ids, Category.all, :id, :name

```
            #
            # f.association :categories,
            # as: :check_boxes,
            # label_method: :name,
            # value_method: :id,
            # label: 'Select Category'
            %>

```

This is basically all the code about this problem. If you can send me  
some  
suggestions how to solve it, or some tutorials about this kind of  
situations I would be grateful.

Thanks.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [November 26, 2013, 11:21pm UTC](https://www.ruby-forum.com/t/has-many-through/237733/2 "2013-11-26T23:21:32Z")

</div>

On Mon, Nov 25, 2013 at 3:49 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is basically all the code about this problem.

What problem?

–  
Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[Hassan Schroeder | about.me](http://about.me/hassanschroeder)  
twitter: @hassan
