Hello rails enthusiast, This blog is result of one of my project where I was trying to do a multi-selection box functionality in rails 3.Enough of talking lets ride this rails topic.
Here we consider a general scenario where we need to fetch categories on to the product form & also we able to select the multiple categories for the single product & save it to the database.
Let's take two models Product & Category & we also need one join table of product & category.
for creating join table we use the following command -
It will create a new migration file for us, open this newly created migration file using any of the editor & edit it as shown below .
Here we consider a general scenario where we need to fetch categories on to the product form & also we able to select the multiple categories for the single product & save it to the database.
Let's take two models Product & Category & we also need one join table of product & category.
for creating join table we use the following command -
It will create a new migration file for us, open this newly created migration file using any of the editor & edit it as shown below .
after this run the
This command will create a intermediate table for product & category.
on product.rb model file we need to specify the association
Now we need to fetch the category on to the product table & also a multi-selection of categories should be possible.
So move on to the product_controller.rb file & in create & update method use the following code
and on view files of product specially on _form.html.erb partial where we are accessing the categories we can use the select_tag like following