diff options
author | Joshua Bates <joshuabates@gmail.com> | 2008-04-17 11:58:32 -0700 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-04-23 20:18:09 +1200 |
commit | b6aa0e13b4e590b82550a6464924f431e57229df (patch) | |
tree | 48afd640a589e8d727ec52eda8daadc29a183356 /activerecord | |
parent | ae51013c3f7b8a8579fcb99d889ed80e9dd75797 (diff) | |
download | rails-b6aa0e13b4e590b82550a6464924f431e57229df.tar.gz rails-b6aa0e13b4e590b82550a6464924f431e57229df.tar.bz2 rails-b6aa0e13b4e590b82550a6464924f431e57229df.zip |
Fix include? on has_many collections with finder_sql to fall back to Array include? rather than try to use SQL.
[#18 state:resolved]
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 11 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 9c724d2117..7e47bf7bdf 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -214,6 +214,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? 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 6f0190e8d9..7b97afe42c 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -832,6 +832,17 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert ! firm.clients.loaded? end + def test_include_loads_collection_if_target_uses_finder_sql + firm = companies(:first_firm) + client = firm.clients_using_sql.first + + firm.reload + assert ! firm.clients_using_sql.loaded? + assert firm.clients_using_sql.include?(client) + assert firm.clients_using_sql.loaded? + end + + def test_include_returns_false_for_non_matching_record_to_verify_scoping firm = companies(:first_firm) client = Client.create!(:name => 'Not Associated') |