Fix include? on has_many collections with finder_sql to fall back to Array include? rather than try to use SQL.

[#18 state:resolved]
This commit is contained in:
Joshua Bates 2008-04-17 11:58:32 -07:00 committed by Michael Koziarski
parent ae51013c3f
commit b6aa0e13b4
2 changed files with 12 additions and 0 deletions

View File

@ -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

View File

@ -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')