There will then be one column for “ commentable_id”, which will store the ID of the object we’re commenting on, and then “ commentable_type”, which will indicate what type of object we’re commenting on (in this case, a Story or a Comment). With a polymorphic association, we’ll create one Comments model. Or you could use polymorphic associations. To build this, you could also use one model, with one column for “ story_id” and one for “ comment_id” (meaning one column would always be nil). Other users can then comment directly on that story, or comment on another user’s comment. We’ll assume your project involves someone submitting a story link to share on the site. Let’s use reddit as the starting point for this tutorial. Massive platforms like YouTube and reddit are driven by their threaded commenting features. When comments are nested, or threaded, they also give users a chance to engage directly with one another, creating lively discussions and debates. They give users a chance to share their thoughts about a blog post, video, or piece of content. ApproachĬomments are an important part of building engaging communities online. And the tutorial below is fairly straightforward, and most importantly, it works. But it’s always good to build things from scratch when learning, so you have a good understanding of how things work. You can also find gems that offer similar solutions. The approach outlined here would likely need to be revisited for performance reasons at scale. I recently implemented threaded commenting using polymorphic associations in my coding project, and share the details below in the spirit of reciprocal generosity.Īs someone new to Rails, this comes with the disclaimer that there are many other ways to build this. While still very much a newbie myself, I feel I now have an opportunity to give back a bit and write my first tutorial. And the majority of that content has been shared for no other reason than a passion and enthusiasm for helping others. Without the wealth of online tutorials, blog posts, and StackOverflow answers, this journey would be exponentially more difficult. “It takes a village.” It’s an appropriate way to describe learning to code. Mark and his co-founders are learning how to code through Codementor’s Featured Star program, and this tutorial is based on what he has learned so far. The tutorial was written by Mark Webster from the Shipyard Team. This article will walk you through how to create threaded comments in rails using polymorphic associations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |