I’m using acts_as_commentable to in an application through a polymorphic association and wanted to allow comments to be threaded. I’ve got this working, but now I want to create an index action that will return a tree like structure and avoid the N+1 query issue. I have:
item.comments.includes(:comments)
However, it runs the following query which doesn’t do any eager loading:
SELECT "comments".* FROM "comments" WHERE ("comments".commentable_id = 22 AND "comments".commentable_type = 'Item')
Any way to get this to run on a self referencing polymorphic association?
Instead of making comments commentable themselves, you could have an easier time if you have two
belongs_toassociations inComment. One for the original commentable, another for the parent comment in the thread.