Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Nested objects return extra nil array

#5859

Nested objects in 3.0.1 seem to have some sort bug that returns a null object.

Posts class :has_many comments
Comments class :belongs_to post

In routes.rb I have set up

resources :posts do
   resources :comments
end

I have 1 post and 1 comment. In console, when I do a Posts.comments.count, I get 1. Even in my view (comments are displayed on the show_post.html) I do a @post.comments.count which claims 1, but I'm still getting a comment returned to my web program (every field in the comment hash is nil except for the post_id field). Even when I have NO comments in the database, I get an extra comment object with everything set to nil but the post_id. I've cleared and repopulated my database so I know it's not a data issue. Confirmed in my WEBrick server window when I told my script to put comments.

[#<Comment id: 1, name: "test", body: "test comment", post_id: 1, created_at: "2010-10-22 16:52:42", updated_at: "2010-10-22 16:53:28">, #<Comment id: nil, name: nil, body:nil, post_id: 1, created_at: nil, updated_at: nil>]
Even though Comment.count for my whole database is 1 in both console and in browser.

Might not be a bug, but I can't think of anything else at this point, especially since a co-worker confirmed the same thing happening to them.

Reported by brpratt (at gmail) · October 22nd, 2010 @ 09:28 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Denis Odorcic
    Denis Odorcic

    Can't reproduce unfortunately, anyone else?

    October 23rd, 2010 @ 04:05 AM

  2. Aditya Sanghi
    Aditya Sanghi
    • State changed from new to needs-more-info
    • Importance changed from to Low

    It looks like you're doing a build on a post by default somewhere (perhaps a before_filter somewhere?). Please post part of your controller/model code and clear steps to reproduce the error.

    October 23rd, 2010 @ 03:52 PM

  3. Jeff Kreeftmeijer
  4. brpratt (at gmail)
    brpratt (at gmail)

    If it was doing a build by default wouldn't that be reflected in the db? Why would Comment.count show 0 and then my app gets an extra comment?

    This app is pretty much word-for-word from the rails guide for the blog app...
    http://guides.rubyonrails.org/getting_started.html

    I'll repost critical elements here to make it easier.

    comments controller:

    class CommentsController < ApplicationController
     def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment])
      redirect_to post_path(@post)
     end
     def destroy
      @post = Post.find(params[:post_id])
      @comment = @post.comments.find(params[:id])
      @comment.destroy
      redirect_to post_path(@post)
     end
    end
    
    comments model:
    class Comment < ActiveRecord::Base
     belongs_to :post
    end
    
    posts model:
    class Post < ActiveRecord::Base
     validates :name,  :presence => true
     validates :title, :presence => true,
     has_many :comments
    end
    

    October 25th, 2010 @ 03:35 PM

  5. brpratt (at gmail)
    brpratt (at gmail)

    Interestingly I found in the form:

    <h2>Add a comment:</h2>
    <%= form_for([@post, @post.comments.build]) do |f| %>
      <%= f.error_messages %>
    
      <div class="field">
        <%= f.label :commenter %><br />
        <%= f.text_field :commenter %>
      </div>
      <div class="field">
        <%= f.label :body %><br />
        <%= f.text_area :body %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    If I changed the @post.comments.build object to @post.comments.new, it stopped returning a nil object in the array.

    October 25th, 2010 @ 03:47 PM

  6. brpratt (at gmail)
    brpratt (at gmail)

    Interestingly I found in the form:

    <h2>Add a comment:</h2>
    <%= form_for([@post, @post.comments.build]) do |f| %>
      <%= f.error_messages %>
    
      <div class="field">
        <%= f.label :commenter %><br />
        <%= f.text_field :commenter %>
      </div>
      <div class="field">
        <%= f.label :body %><br />
        <%= f.text_area :body %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    If I changed the @post.comments.build object to @post.comments.new, it stopped returning a nil object in the array.

    October 25th, 2010 @ 03:47 PM

  7. Aditya Sanghi
    Aditya Sanghi
    • State changed from needs-more-info to invalid

    Marking ticket as invalid for now as it's clear where your object is coming from.

    Please see documentation related to active record. It is clear that @post.comments.build is the way to build associations (not new).

    Doing build will create an object but not save it of course.

    October 25th, 2010 @ 04:59 PM

  8. bingbing