title: Rails nested attributes - how to add category attribute to new product?
body: <p>I'm using rails to create a new product and want to add a category to every product. </p>
  
  <p>I have three tables: product, category, and categorizations (which stores the relationship between products and categories). I'm trying to use nested attributes to manage the creation of the categorizations, but unsure how my controller and view/form should be updated so that new products also update the categorizations table. </p>
  
  <p>Here are my models: </p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
   belongs_to :users
   has_many :categorizations
   has_many :categories, :through =&gt; :categorizations
   has_attached_file :photo
   accepts_nested_attributes_for :categorizations, allow_destroy: true
  
   attr_accessible :description, :name, :price, :photo
  
   validates :user_id, presence: true
  
  end
  
  
  class Category &lt; ActiveRecord::Base
   attr_accessible :description, :name, :parent_id
   acts_as_tree
   has_many :categorizations, dependent: :destroy
   has_many :products, :through =&gt; :categorizations
  
  end
  
  
  class Categorization &lt; ActiveRecord::Base
    belongs_to :category
    belongs_to :product
    attr_accessible :category_id, :created_at, :position, :product_id
  
  end
  </code></pre>
  
  <p>Here is my new product controller: </p>
  
  <pre><code>def new
      @product = Product.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @product }
      end
    end
  </code></pre>
  
  <p>And here is my view form: </p>
  
  <pre><code>&lt;%= form_for @product, :html =&gt; { :multipart =&gt; true } do |f| %&gt;
    &lt;% if @product.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@product.errors.count, "error") %&gt; prohibited this product from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @product.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :description %&gt;&lt;br /&gt;
      &lt;%= f.text_field :description %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :price %&gt;&lt;br /&gt;
      &lt;%= f.number_field :price %&gt;
    &lt;/div&gt;
  &lt;div class="field"&gt;
  &lt;%= f.file_field :photo %&gt;
  &lt;/div&gt;
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>How should I update my controller so that both the product and the categorization tables are updated when a new product is added? How do I update my view file so that the categories appear in a drop down menu? </p>