1
\$\begingroup\$

I'm trying to connect the create of a comment to the current_user. I have a model comment which is a polymorphic association.

class CreateComments < ActiveRecord::Migration[6.0]
  def change
    create_table :comments do |t|
      t.string :content
      t.references :commentable, polymorphic: true
      t.timestamps
    end
  end
end

In the controller I'm trying to connect the @comment.user = current_user. controllers/comments_controller.rb

  def create
    @comment = @commentable.comments.new comment_params
    @comment.user = current_user
    @comment.save
    redirect_to @commentable, notice: 'Comment Was Created'
  end

But I get the following error:

NoMethodError: undefined method `user=' for # Did you mean? user_id=

I would rather set up a relationship from which I can get to a user (The creator of comment) from the comment itself. For example @comment.user instead of just having the id.

I have the following models:

class Question < ApplicationRecord
  belongs_to :user
  has_many :comments, as: :commentable
  has_rich_text :content
end

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :posts
  has_many :questions
  has_one_attached :avatar

  before_create :set_defaults

  def set_defaults
    self.avatar = 'assets/images/astronaut.svg' if self.new_record?
  end
end

class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

Since comment is a polymorphic association, I'm not sure if it should be connected to user via a belongs_to to user.

[5] pry(#<Questions::CommentsController>)> @comment
=> #<Comment:0x00007fb3e6df72e8
 id: nil,
 content: "some test content",
 commentable_type: "Question",
 commentable_id: 1,
 created_at: nil,
 updated_at: nil,
 user_id: nil>
[6] pry(#<Questions::CommentsController>)>

Does this mean, I should create a foreign_key in the comments migration to a user? How can I go about getting the creator of the comment from the comment itself? Something like @comment.creator or @comment.user?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Don't think your question belongs to this code review section. You would have gotten help much faster if had posted in stackoverflow instead. Anyway there are couple points you need to check.

  1. The CreateComments migration must have been updated (or at least there was another migration that changed the table) because the migration does not say anything about user_id column but the your model has that.

  2. Since comment is a polymorphic association, I'm not sure if it should be connected to user via a belongs_to to user. You are confused between the polymorphic association relation and the user's relation of a comment. They are different, a comment belongs to a Question (or Post, Thread... that why you have polymorphic here) and also belongs to a user who created the comment. Even better, you can have another polymorphic association for commenter (I will skip it to avoid adding noise to your question)

  3. Does this mean, I should create a foreign_key in the comments migration to a user? NO

  4. How can I go about getting the creator of the comment from the comment itself? Something like @comment.creator or @comment.user? They are just methods rails generated for you after you setup the association, so yeah you can have whatever you want

Here is what I would do to get comment.commenter and comment.commenter= to work:

class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
  belongs_to :commenter, class_name: 'User', foreign_key: 'user_id'
end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.