This project is archived and is in readonly mode.

#3472 ✓resolved
Dmitry Ratnikov

AssociationCollection#include? ignoring build associated objects

Reported by Dmitry Ratnikov | November 9th, 2009 @ 05:59 AM | in 2.3.10

Consider:

class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

The following test fails:


def test_build
  post = Post.new
  comment = post.comments.build
  assert post.comments.include?(comment)
end

The logs show:


  Comment Load (0.2ms)   SELECT "comments".id FROM "comments" WHERE ("comments"."id" = NULL) AND ("comments".post_id = NULL) LIMIT 1

It seems that the collection ignores that it has been setup (loaded? #=> false) and reloads it from the database. Regarding that the database is a new record and hence definitely cannot be in the database, that is probably not expected behavior.

Any suggestions?

Comments and changes to this ticket

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>

Referenced by

Pages