Skip to content

Commit

Permalink
Fix include? on has_many collections with finder_sql to fall back to …
Browse files Browse the repository at this point in the history
…Array include? rather than try to use SQL.

[#18 state:resolved]
  • Loading branch information
joshuabates authored and NZKoz committed Apr 23, 2008
1 parent ae51013 commit b6aa0e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Expand Up @@ -214,6 +214,7 @@ def replace(other_array)

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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -832,6 +832,17 @@ def test_include_checks_if_record_exists_if_target_not_loaded
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')
Expand Down

0 comments on commit b6aa0e1

Please sign in to comment.