Rails 4 collection_check_boxes, with has_many via

I am trying to associate categories with products. So far i have implemented this

Class Product
    has_many :categorizations
    has_many :categories, through: :categorizations

.

Class Categorization
    belongs_to :product
    belongs_to :category

.

Class Category
    has_many :categorizations
    has_many :products, through: :categorizations

and in my products /_form.html.erb

<div class="field">
<%= f.label :category_id %><br />
<%= collection_check_boxes(:product, :category_id, Category.all, :id, :name) %>
</div>

I'm not sure how to do it right.

Solution
Change: :category_idto :category_idsand set strong parameters

def product_params
  params.require(:product).permit(:title, :description, :price, :category_ids => [])
end
+4
source share
1 answer

Being the fact that the relation is ambiguous, this product should respond to category_ids(plural), and not category_id(singularly).

+6
source

All Articles