Ask A Question


You’re not receiving notifications from this thread.

How do I make comments show the user who created it?

Wesly Mezarina asked in General

I would think that i could do something like the code below but it's not working. How can i approach this so it can show the user who created the comment and other information about the user? I did follow the tutorial video on polymorphic associations if that helps.

 - commentable.comments.each do |comment|
        = image_tag comment.user.avatar.url(:medium)
        = comment.user.username
           = comment.created_at.strftime("%b, %d")
          = comment.body

You can do exactly what you've got here with the association on the comments. What's not working?


I get this error

 undefined method `user' for #<Comment:0x007fc94a46b340>

Ah hmm, I can't remember, did that episode include adding a user_id column and belongs_to :user association to the comments? I thought it did but it's been a while. :)

If you don't have that, you'll need to do that to reference the user and that should do the trick.


I'm not sure how i missed that simple mistake. Thanks!

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 78,890+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.