This project is archived and is in readonly mode.

#5859 ✓invalid
brpratt (at gmail)

Nested objects return extra nil array

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

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.

Comments and changes to this ticket

  • Denis Odorcic

    Denis Odorcic October 23rd, 2010 @ 04:05 AM

    Can't reproduce unfortunately, anyone else?

  • Aditya Sanghi

    Aditya Sanghi October 23rd, 2010 @ 03:52 PM

    • 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.

  • Jeff Kreeftmeijer
  • brpratt (at gmail)

    brpratt (at gmail) October 25th, 2010 @ 03:35 PM

    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
    
  • brpratt (at gmail)

    brpratt (at gmail) October 25th, 2010 @ 03:47 PM

    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.

  • brpratt (at gmail)

    brpratt (at gmail) October 25th, 2010 @ 03:47 PM

    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.

  • Aditya Sanghi

    Aditya Sanghi October 25th, 2010 @ 04:59 PM

    • 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.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages