Unless I'm overlooking something, it seems like you have some duplicate code in the video:
def comments Comment.where(commentable: commentable, parent_id: id) end def child_comments Comment.where(parent: self) end
While they use slightly different
where's the end result will be the same. The
commentable: commentable condition is unnecessary, as there should never exist a comment that has a different commentable than its parent.
If you're using Postgres you're likely to run into an issue with this solution due to the following columns being generated when creating the schema:
t.bigint "user_id", null: false t.string "commentable_type", null: false t.bigint "commentable_id", null: false
This means when you delete a comment you get the following error:
ERROR: null value in column "user_id" violates not-null constraint
The workaround that I've implemented is to add a
deleted column that defaults to
false on the
This means we can set deleted to true:
def destroy update(deleted: true) end
<% if comment.deleted == true %> <h5 class="text-semibold">[Deleted]</h5> <p>[deleted]</p> <% else %> <h5 class="text-semibold"><%= comment.user.name %> posted:</h5> <%= simple_format(comment.body) %> <% end %>
Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.