Search code examples
ruby-on-railsrubypolymorphic-associations

Rails: polymorphic associations, find commented on


I've implemented polymorphic comments on my Rails app using this tutorial.

So I have:

class Place < ApplicationRecord
  has_many :comments, as: :commentable
end

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

Now I want to index all comments and join them with the places they comment on, but I have no idea on how to achieve this.

In my comments_controller.rb I have:

class CommentsController < ApplicationController
  def index
    @comments = Comment.all
  end
end

And in my comments index.html.erb I can access:

<%= comment.commentable_type %>
<%= comment.commentable_id %>

But how do I access the actual attributes of what the comments are on, example: <%= comment.place.name %>?

I believe it is related to this answer but I don't know how: rails joins polymorphic association


Solution

  • You can refer to the commentable object with just

    comment.commentable
    

    The only thing to watch out for here is that it assumes that all your commentables have the method you call on them, such as name.

    comment.commentable.name
    

    You will want to preload these in your controller to avoid an N+1 as well.

    class CommentsController < ApplicationController
      def index
        @comments = Comment.includes(:commentable).all
      end
    end