title: Rails Associations (belongs_to, has_many) can't save 2 ids in table with a create method (user, post, comment)
body: <p>Trying to write a basic "blog-like" app in rails 3, I'm stuck with associations. I need the create method save the post_id as well as the user_id in the comment table (which I need in order to retrive all comments written by a user in order to display it)</p>
  
  <p>The app has users (authentication - devise), posts (posted by users - but I'm not sure it matters in my case) and comments (on the posts, posted by users).</p>
  
  <p>the comment table has a post_id, a body, and also a user_id</p>
  
  <p>Associations:</p>
  
  <pre><code>has_many :comments (In the Post model)
  belongs_to :post (In the Comment model)
  belongs_to :user (In the Comment model)
  has_many :comments (In the User model)
  </code></pre>
  
  <p>the routes:</p>
  
  <pre><code>resources :posts do
    resources :comments
  end
  
  resources :users do
    resources :comments
  end
  </code></pre>
  
  <p>The comment post form displayed on the posts show view: (posts/show.html.erb)</p>
  
  <pre><code>&lt;% form_for [@post, Comment.new] do |f| %&gt;
    &lt;%= f.label :body %&gt;
    &lt;%= f.text_area :body %&gt;
    &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>and finally, the create method in the comments controller:</p>
  
  <p>A.) If I write this a post_id is written in the database</p>
  
  <pre><code>def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(params[:comment])
    redirect_to @post
  end
  </code></pre>
  
  <p>B.) If I write this a user_id is written...</p>
  
  <pre><code>def create
    @user = current_user
    @comment = @user.comments.create!(params[:comment])
    redirect_to @post
  end
  </code></pre>
  
  <p>I tried:</p>
  
  <pre><code>@comment = @post.comments.create!(params[:comment].merge(:user =&gt; current_user))
  </code></pre>
  
  <p>But it doesn't work.. How can I write a method which save the user_id and the post_id ? Did I have also to do some change in the comment post form (something like &lt;% form_for [@post, @user, Comment.new] do |f| %> ?)</p>
  
  <p>Thank you!</p>