title: Add Comment to User and Post models (Ruby on Rails)
body: <p>I'm new to Rails. I'm building my first app - simple blog. I have User and Post models, where each user can write many posts. Now I want to add Comment model, where each post can have many comments, and also each user can comment on any post created by any other user.<br>
  In Comment model I have</p>
  
  <pre>id \ body \ user_id \ post_id</pre>
  
  <p>columns. <br>
  Model associations: <br>
  <strong>user.rb</strong></p>
  
  <pre><code>has_many :posts,    dependent: :destroy
  has_many :comments
  </code></pre>
  
  <p><strong>post.rb</strong></p>
  
  <pre><code>has_many :comments, dependent: :destroy
  belongs_to :user
  </code></pre>
  
  <p><strong>comment.rb</strong></p>
  
  <pre><code>belongs_to :user
  belongs_to :post
  </code></pre>
  
  <p>So how do I correctly define create action in CommentsController?
  Thank you.</p>
  
  <p><strong>UPDATE:</strong> <br>
  routes.rb</p>
  
  <pre><code>resources :posts do
    resources :comments
  end
  </code></pre>
  
  <p>comments_controller.rb</p>
  
  <pre><code>  def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(comment_params)
      if @comment.save
        redirect_to @post
      else
        flash.now[:danger] = "error"
      end
    end
  </code></pre>
  
  <p>The result is</p>
  
  <pre><code>--- !ruby/hash:ActionController::Parameters
  utf8: ✓
  authenticity_token: rDjSn1FW3lSBlx9o/pf4yoxlg3s74SziayHdi3WAwMs=
  comment: !ruby/hash:ActionController::Parameters
    body: test
  action: create
  controller: comments
  post_id: '57'
  </code></pre>
  
  <p>As we can see it doesnt send user_id and works only if I delete <code>validates :user_id, presence: true</code> string from comment.rb</p>
  
  <p>Any suggestions?</p>