require 'spec_helper'

describe Product do

  describe 'update product category through ids' do

    it 'works' do
      supplier = create :supplier
      pc1 = create :product_category, supplier: supplier
      pc2 = create :product_category, supplier: supplier

      product = build :product, supplier: supplier, product_category_ids: [pc1.id]
      product.save.should be true
      product.reload
      pc1.reload
      pc2.reload
      pc1.product_ids.should == [product.id]
      pc2.product_ids.should_not be_present

      product.update_attributes product_category_ids: [pc2.id]
      product.reload
      pc1.reload
      pc2.reload
      product.product_categories.should == [pc2]

      # empty set also works
      product.update_attributes product_category_ids: ['']
      product.reload
      product.product_categories.should be_empty
    end
  end


end
