has_many :through

17 views
Skip to first unread message

vuk...@gmail.com

unread,
Nov 25, 2013, 6:49:12 PM11/25/13
to rubyonra...@googlegroups.com
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.




Hassan Schroeder

unread,
Nov 26, 2013, 5:20:22 PM11/26/13
to rubyonrails-talk
On Mon, Nov 25, 2013 at 3:49 PM, <vuk...@gmail.com> wrote:

> This is basically all the code about this problem.

What problem?

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Reply all
Reply to author
Forward
0 new messages