Ruby

Trent Janckila
Trent Janckila
4,749 Points

@post.comments.length is 1 when no comments exist

I'm attempting to add/edit comments added to the blog site that has been being created, and I keep having a strange error.

Even though there are no comments associated with a post, the post still has a comments array of length 1. This means that when attempting to render the post page (or render any page that includes the comment partial), rails attempts to render a comment with a comment_id = nil. As one might expect, this plays merry havoc with my attempts to put the blog together.

I came across another question ( https://teamtreehouse.com/community/no-route-matches-actionedit-controllercomments-idnil-postid2-missing-required-keys-id ), which I assume has the same problem, but there was no resolution.

Where would one start looking to discover this mystery element?

TLDR version: Rails is showing an extra element in all post comments which is breaking the blog app.

Trent Janckila
Trent Janckila
4,749 Points

To add on some more to this, the mystery length does not appear in the rails console.

irb(main):001:0> post = Post.first
   (0.4ms)  SELECT sqlite_version(*)
  Post Load (0.1ms)  SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ?  [["LIMIT", 1]]
irb(main):002:0> post.comments.length
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?  [["post_id", 2]]
=> 0

The mystery array element only appears when accessing the comments from the rails blog. Here's the logging from the rails log.

Started GET "/posts/2" for ::1 at 2020-01-11 08:24:10 -0700
Processing by PostsController#show as HTML
  Parameters: {"id"=>"2"}
  Post Load (0.2ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
   app/controllers/posts_controller.rb:67:in `set_post'
  Rendering posts/show.html.erb within layouts/application
  Rendered comments/_form.html.erb (Duration: 3.3ms | Allocations: 2650)
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?  [["post_id", 2]]
  ↳ app/views/posts/show.html.erb:17
#<Comment id: nil, content: nil, name: nil, post_id: 2, created_at: nil, updated_at: nil>
  Rendered comments/_comment.html.erb (Duration: 0.5ms | Allocations: 273)
  Rendered posts/show.html.erb within layouts/application (Duration: 15.7ms | Allocations: 12104)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 21ms (Views: 18.0ms | ActiveRecord: 0.9ms | Allocations: 17193)

1 Answer

Trent Janckila
Trent Janckila
4,749 Points

I found the issue. I was creating a new comment form to display before the comments were being displayed on the page. This was creating the empty comment causing problems a few lines later when it grabbed all comments associated with the post. I've changed it for now so I'll be able to continue through the course, but I'd prefer to be able to have the "add a comment" form before the comments.

before (how I'd like it)

<div id="comments">
    <h1>Comments</h1>

    <h2>Add a Comment</h2>
    <%= render partial: "comments/form", locals: {comment: @post.comments.new} %>

    <% @post.comments.each do |comment| %>
    <%= render partial: "comments/comment", locals: {comment: comment} %>
    <% end %>
</div>

After (now working)

<div id="comments">
    <h1>Comments</h1>

    <% @post.comments.each do |comment| %>
    <%= render partial: "comments/comment", locals: {comment: comment} %>
    <% end %>

    <h2>Add a Comment</h2>
    <%= render partial: "comments/form", locals: {comment: @post.comments.new} %>
</div>