title: "Create" Action for nested resource with multiple Associations in Rails
body: <p>I have <code>item</code>s, that each have multiple (threaded) <code>comment</code>s.<br>
  The threading is done via a <code>parent</code> key, that points towards another <code>comment</code>.</p>
  
  <p>I just can't get the <code>create</code> action to work properly, I got it far enough to submit into the database, but it didn't have <code>item_id</code> and <code>parent_id</code> set.</p>
  
  <p>I tried <code>form_for [@item, @comment]</code> instead of <code>form_for :comment, url: item_comments_path( @item )</code> but it didn't help either.</p>
  
  <p>When I look at <code>comment_params</code> in the <code>create</code> action I get this:</p>
  
  <pre><code>Parameters: {"utf8"=&gt;"✓", "comment"=&gt;{"body"=&gt;"examplecommentgoeshere"}, "parent_id"=&gt;"4", "commit"=&gt;"Create Comment", "item_id"=&gt;"4"}
  </code></pre>
  
  <p>Can anyone help?</p>
  
  <h3>Models:</h3>
  
  <pre><code>class Item &lt; ActiveRecord::Base
    belongs_to :user
    has_many :comments, dependent: :destroy
    has_many :images,   as: :item,
                        dependent: :destroy
  
    validates :title,       presence: true,
                            allow_blank: false
    validates :description, presence: true,
                            allow_blank: false
    validates :user,        presence: true,
                            allow_blank: false
    validates :layout,        presence: true
  end
  
  
  class Comment &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :item
    has_many :responses,  class_name: "Comment",
                          foreign_key: "parent_id",
                          dependent: :destroy
    has_one :image,       as: :item,
                          dependent: :destroy
    belongs_to :parent,   class_name: "Comment"
  
    validates :body,      presence: true,
                          allow_blank: false,
                          length: { minimum: 10 }
  end
  </code></pre>
  
  <h3>Comment Controller:</h3>
  
  <pre><code>class CommentsController &lt; ApplicationController
    before_filter :findParent
    before_filter :find, only: [:update, :destroy, :show]
    before_filter :is_logged_in?, only: [:create, :update, :destroy]
    before_filter :has_permission?, only: [:update, :destroy]
  
    def new
      @comment = Comment.new
    end
  
    def create
      logger.debug comment_params.inspect
      @comment = current_user.comments.build(comment_params)
  
      if @comment.save
        redirect_to @item
      else
        logger.debug @comment.errors.inspect
        session[:errorbj] = @comment
        redirect_to @item
      end
    end
    [...]
    private
    def comment_params
      params.require(:comment).permit(:body, :parent)
    end
  
    private
    def find
      @comment = Comment.find(params[:id])
    end
  
    private
    def findParent
      @item = Item.find params[:item_id]
    end
  </code></pre>
  
  <h3>Item View:</h3>
  
  <pre><code>&lt;p&gt;
  &lt;h3&gt;Add a comment:&lt;/h3&gt;
    &lt;%= render partial: 'comments/form', locals: { parent: @item } %&gt;
  &lt;/p&gt;
  </code></pre>
  
  <h3>Partial comments/form:</h3>
  
  <pre><code>&lt;%= form_for :comment, url: item_comments_path( @item ) do |f| %&gt;
    &lt;p&gt;
      &lt;%= f.label :body %&gt;&lt;br&gt;
      &lt;%= f.text_area :body %&gt;
    &lt;/p&gt;
  
    &lt;p&gt;
      &lt;%= f.label :image %&gt;&lt;br&gt;
      &amp;lt;nope&amp;gt;
    &lt;/p&gt;
  
    &lt;%= hidden_field_tag :parent_id, parent.id %&gt;
    &lt;p&gt;
      &lt;%= f.submit %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>From another stackoverflow thread I concluded that the only way to create an object in a multi-association is to do something along the lines of this:</p>
  
  <pre><code>@comment = current_user.comments.build( comment_params.join( { parent_id: &lt;parent.id&gt; } ) )
  </code></pre>
  
  <p>Other stackoverflow posts however said not to do</p>
  
  <pre><code>@item = Item.new( item_params.join( { user_id: current_user.id } )
  </code></pre>
  
  <p>So is there really no better way?</p>