Skip to content
This repository
Browse code

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
13 activerecord/lib/active_record/associations/association_collection.rb
@@ -332,6 +332,7 @@ def replace(other_array)
332 332
333 333 def include?(record)
334 334 return false unless record.is_a?(@reflection.klass)
  335 + return include_in_memory?(record) if record.new_record?
335 336 load_target if @reflection.options[:finder_sql] && !loaded?
336 337 return @target.include?(record) if loaded?
337 338 exists?(record)
@@ -503,6 +504,18 @@ def fetch_first_or_last_using_find?(args)
503 504 args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] ||
504 505 @target.any? { |record| record.new_record? } || args.first.kind_of?(Integer))
505 506 end
  507 +
  508 + def include_in_memory?(record)
  509 + if @reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
  510 + @owner.send(proxy_reflection.through_reflection.name.to_sym).each do |source|
  511 + source_reflection_target = source.send(proxy_reflection.source_reflection.name)
  512 + return true if source_reflection_target.respond_to?(:include?) ? source_reflection_target.include?(record) : source_reflection_target == record
  513 + end
  514 + false
  515 + else
  516 + @target.include?(record)
  517 + end
  518 + end
506 519 end
507 520 end
508 521 end
5 activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -819,4 +819,9 @@ def test_caching_of_columns
819 819 assert_queries(0) { david.projects.columns; david.projects.columns }
820 820 end
821 821
  822 + def test_include_method_in_has_and_belongs_to_many_association_should_return_true_for_instance_added_with_build
  823 + project = Project.new
  824 + developer = project.developers.build
  825 + assert project.developers.include?(developer)
  826 + end
822 827 end
7 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1221,5 +1221,10 @@ class NullifyModel < ActiveRecord::Base
1221 1221 end
1222 1222 EOF
1223 1223 end
1224   -end
1225 1224
  1225 + def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
  1226 + post = Post.new
  1227 + comment = post.comments.build
  1228 + assert post.comments.include?(comment)
  1229 + end
  1230 +end
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
343 343 lambda { authors(:david).very_special_comments.delete(authors(:david).very_special_comments.first) },
344 344 ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
345 345 end
  346 +
  347 + def test_include_method_in_association_through_should_return_true_for_instance_added_with_build
  348 + person = Person.new
  349 + reference = person.references.build
  350 + job = reference.build_job
  351 + assert person.jobs.include?(job)
  352 + end
  353 +
  354 + def test_include_method_in_association_through_should_return_true_for_instance_added_with_nested_builds
  355 + author = Author.new
  356 + post = author.posts.build
  357 + comment = post.comments.build
  358 + assert author.comments.include?(comment)
  359 + end
346 360 end

0 comments on commit 96c19ff

Please sign in to comment.
Something went wrong with that request. Please try again.