diff options
author | Marcelo Giorgi <marklazz.uy@gmail.com> | 2010-09-30 14:56:11 -0300 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-09-30 12:13:52 -0700 |
commit | ef6df93a8ddb675f1298973bb347825c554e95f9 (patch) | |
tree | 2d2f9ac60ae8f3d07fa330f094bf66f0f40272dc /activerecord/lib/active_record | |
parent | 505b532605bd3b9b8a444be10911f0864f1d42ba (diff) | |
download | rails-ef6df93a8ddb675f1298973bb347825c554e95f9.tar.gz rails-ef6df93a8ddb675f1298973bb347825c554e95f9.tar.bz2 rails-ef6df93a8ddb675f1298973bb347825c554e95f9.zip |
AssociationCollection#include? working properly for objects added with build method [#3472 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 91e0a9f2f8..a617a0fb36 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -363,6 +363,7 @@ module ActiveRecord 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) @@ -554,6 +555,18 @@ module ActiveRecord 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).map 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 |