Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
delete correct records for a has_many with :primary_key and :dependen…
…t => :delete_all

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
al2o3cr authored and NZKoz committed Nov 10, 2009
1 parent 22e1f4b commit 6c0028d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -1411,7 +1411,7 @@ def configure_dependency_for_has_many(reflection, extra_conditions = nil)
if reflection.options.include?(:dependent) if reflection.options.include?(:dependent)
# Add polymorphic type if the :as option is present # Add polymorphic type if the :as option is present
dependent_conditions = [] dependent_conditions = []
dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}" dependent_conditions << "#{reflection.primary_key_name} = \#{record.#{reflection.name}.send(:owner_quoted_id)}"
dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as] dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
dependent_conditions << sanitize_sql(reflection.options[:conditions], reflection.quoted_table_name) if reflection.options[:conditions] dependent_conditions << sanitize_sql(reflection.options[:conditions], reflection.quoted_table_name) if reflection.options[:conditions]
dependent_conditions << extra_conditions if extra_conditions dependent_conditions << extra_conditions if extra_conditions
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -651,6 +651,18 @@ def test_dependent_association_respects_optional_hash_conditions_on_delete
assert_equal 1, Client.find_all_by_client_of(firm.id).size assert_equal 1, Client.find_all_by_client_of(firm.id).size
end end


def test_delete_all_association_with_primary_key_deletes_correct_records
firm = Firm.find(:first)
# break the vanilla firm_id foreign key
assert_equal 2, firm.clients.count
firm.clients.first.update_attribute(:firm_id, nil)
assert_equal 1, firm.clients(true).count
assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
old_record = firm.clients_using_primary_key_with_delete_all.first
firm = Firm.find(:first)
firm.destroy
assert Client.find_by_id(old_record.id).nil?
end


def test_creation_respects_hash_condition def test_creation_respects_hash_condition
ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/reflection_test.rb
Expand Up @@ -170,8 +170,8 @@ def test_association_reflection_in_modules


def test_reflection_of_all_associations def test_reflection_of_all_associations
# FIXME these assertions bust a lot # FIXME these assertions bust a lot
assert_equal 35, Firm.reflect_on_all_associations.size assert_equal 36, Firm.reflect_on_all_associations.size
assert_equal 25, Firm.reflect_on_all_associations(:has_many).size assert_equal 26, Firm.reflect_on_all_associations(:has_many).size
assert_equal 10, Firm.reflect_on_all_associations(:has_one).size assert_equal 10, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end end
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/company.rb
Expand Up @@ -64,6 +64,8 @@ class Firm < Company
has_many :readonly_clients, :class_name => 'Client', :readonly => true has_many :readonly_clients, :class_name => 'Client', :readonly => true
has_many :clients_using_primary_key, :class_name => 'Client', has_many :clients_using_primary_key, :class_name => 'Client',
:primary_key => 'name', :foreign_key => 'firm_name' :primary_key => 'name', :foreign_key => 'firm_name'
has_many :clients_using_primary_key_with_delete_all, :class_name => 'Client',
:primary_key => 'name', :foreign_key => 'firm_name', :dependent => :delete_all
has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id" has_many :clients_grouped_by_firm_id, :class_name => "Client", :group => "firm_id", :select => "firm_id"
has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name" has_many :clients_grouped_by_name, :class_name => "Client", :group => "name", :select => "name"


Expand Down

0 comments on commit 6c0028d

Please sign in to comment.