Permalink
Browse files

Fixes CollectionAssociation#ids_reader returning incorrect ids for ne…

…w records
  • Loading branch information...
1 parent f42e0fd commit c2377f72d2c7df1103eac73b0eb3781c02ef2e18 @armstrjare armstrjare committed Jun 10, 2013
@@ -1,5 +1,11 @@
## unreleased ##
+* Prevent query with NULL foreign key value on CollectionAssociation#ids_reader
+ for non-persisted records. Fixes bug where Company.new.contract_ids would
+ incorrectly load all non-associated Contracts.
+
+ *Jared Armstrong*
+
* Fix the `:primary_key` option for `has_many` associations.
Fixes #10693.
@@ -43,7 +43,7 @@ def writer(records)
# Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
def ids_reader
- if loaded? || options[:finder_sql]
+ if owner.new_record? || loaded? || options[:finder_sql]
load_target.map do |record|
record.send(reflection.association_primary_key)
end
@@ -1311,6 +1311,33 @@ def test_get_ids_for_unloaded_associations_does_not_load_them
assert !company.clients.loaded?
end
+ def test_get_ids_for_association_on_new_record_does_not_try_to_find_records
+ Company.columns # Load schema information so we don't query below
+ Contract.columns # if running just this test.
+
+ company = Company.new
+ assert_queries(0) do
+ company.contract_ids
+ end
+
+ assert_equal [], company.contract_ids
+ end
+
+ def test_set_ids_for_association_on_new_record_applies_association_correctly
+ contract_a = Contract.create!
+ contract_b = Contract.create!
+ another_contract = Contract.create!
+ company = Company.new(:name => "Some Company")
+
+ company.contract_ids = [contract_a.id, contract_b.id]
+ assert_equal [contract_a.id, contract_b.id], company.contract_ids
+ assert_equal [contract_a, contract_b], company.contracts
+
+ company.save!
+ assert_equal company, contract_a.reload.company
+ assert_equal company, contract_b.reload.company
+ end
+
def test_get_ids_ignores_include_option
assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
end

1 comment on commit c2377f7

Using scopes with a new record still queries non-associated records. Seems like the bug (around since 3.1 by my count) is a little deeper than what this commit fixed:

company = Company.new # Company has many :contracts
company.contracts.approved # => SELECT ... WHERE `contracts`.`company_id` IS NULL AND ...
Please sign in to comment.