Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

AssociationCollection#include? working properly for objects added wit…

…h build method [#3472 state:resolved]
  • Loading branch information...
commit 96c19ff7cc1521b9f10ffb1004601d70b7025cba 1 parent 9b78af9
@marklazz marklazz authored tenderlove committed
View
13 activerecord/lib/active_record/associations/association_collection.rb
@@ -332,6 +332,7 @@ def replace(other_array)
def include?(record)
return false unless record.is_a?(@reflection.klass)
+ return include_in_memory?(record) if record.new_record?
load_target if @reflection.options[:finder_sql] && !loaded?
return @target.include?(record) if loaded?
exists?(record)
@@ -503,6 +504,18 @@ def fetch_first_or_last_using_find?(args)
args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] ||
@target.any? { |record| record.new_record? } || args.first.kind_of?(Integer))
end
+
+ def include_in_memory?(record)
+ if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
+ @owner.send(proxy_reflection.through_reflection.name.to_sym).each do |source|
+ source_reflection_target = source.send(proxy_reflection.source_reflection.name)
+ return true if source_reflection_target.respond_to?(:include?) ? source_reflection_target.include?(record) : source_reflection_target == record
+ end
+ false
+ else
+ @target.include?(record)
+ end
+ end
end
end
end
View
5 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -819,4 +819,9 @@ def test_caching_of_columns
assert_queries(0) { david.projects.columns; david.projects.columns }
end
+ def test_include_method_in_has_and_belongs_to_many_association_should_return_true_for_instance_added_with_build
+ project = Project.new
+ developer = project.developers.build
+ assert project.developers.include?(developer)
+ end
end
View
7 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1221,5 +1221,10 @@ class NullifyModel < ActiveRecord::Base
end
EOF
end
-end
+ def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
+ post = Post.new
+ comment = post.comments.build
+ assert post.comments.include?(comment)
+ end
+end
View
14 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -343,4 +343,18 @@ def test_modifying_has_many_through_has_one_reflection_should_raise
lambda { authors(:david).very_special_comments.delete(authors(:david).very_special_comments.first) },
].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
end
+
+ def test_include_method_in_association_through_should_return_true_for_instance_added_with_build
+ person = Person.new
+ reference = person.references.build
+ job = reference.build_job
+ assert person.jobs.include?(job)
+ end
+
+ def test_include_method_in_association_through_should_return_true_for_instance_added_with_nested_builds
+ author = Author.new
+ post = author.posts.build
+ comment = post.comments.build
+ assert author.comments.include?(comment)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.