Q:

Why user_id not assigned comment_id? 

I tried to create a blog, and I have 3 models post, user, comment.  When I created comment,  comment.post_id is nil.  What am I doing wrong? 
comments_controller
  def new
    @comment = Comment.new
  end

  def edit
    @post = Post.find(params[:post_id])
  end

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    @comment.user_id = current_user
    redirect_to post_path(@post)
  end

edit
post.rb
  has_many :comments, dependent: :destroy
  belongs_to :user

comment.rb
  belongs_to :user
  belongs_to :post

user.rb
  has_many :posts, dependent: :destroy

  has_many :comments, dependent: :destroy

A:

Here is solution:
def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.new(params[:comment])
  @comment.user_id = current_user.id
  @comment.save
  redirect_to post_path(@post)
end

Source: Answer in stackoverflow
So, use following association as in source above.