From 4e7974db9e1038526090955b37856e28ccb5e5d1 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Sun, 22 Nov 2009 23:27:37 -0200 Subject: [PATCH] AssociationCollection#include? working properly for objects added with build method --- .../associations/association_collection.rb | 2 +- .../associations/has_many_associations_test.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 42b6e5d..dca2cad 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -332,7 +332,7 @@ module ActiveRecord def include?(record) return false unless record.is_a?(@reflection.klass) load_target if @reflection.options[:finder_sql] && !loaded? - return @target.include?(record) if loaded? + return @target.include?(record) if loaded? || record.new_record? exists?(record) end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 2d1a42e..6730ed0 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1138,5 +1138,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_instance_eval_in_association_proxy assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.sort_by(&:id).first.instance_eval{title} end -end + def test_include_method_in_association_proxy_should_return_true_for_instance_added_with_build + post = Post.new + comment = post.comments.build + assert post.comments.include?(comment) + end +end -- 1.6.3.3