mirror of
https://github.com/github/rails.git
synced 2026-01-30 16:58:15 -05:00
Ensure that associations with :dependent => :delete_all respect :conditions option. Closes #8034 [danger, joshpeek, Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6827 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1126,10 +1126,11 @@ module ActiveRecord
|
||||
# delete children, otherwise foreign key is set to NULL.
|
||||
|
||||
# Add polymorphic type if the :as option is present
|
||||
dependent_conditions = %(#{reflection.primary_key_name} = \#{record.quoted_id})
|
||||
if reflection.options[:as]
|
||||
dependent_conditions += " AND #{reflection.options[:as]}_type = '#{base_class.name}'"
|
||||
end
|
||||
dependent_conditions = []
|
||||
dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}"
|
||||
dependent_conditions << "#{reflection.options[:as]}_type = '#{base_class.name}'" if reflection.options[:as]
|
||||
dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
|
||||
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
|
||||
|
||||
case reflection.options[:dependent]
|
||||
when :destroy, true
|
||||
|
||||
@@ -844,6 +844,30 @@ class HasManyAssociationsTest < Test::Unit::TestCase
|
||||
assert Client.find_by_id(client_id).nil?
|
||||
end
|
||||
|
||||
def test_dependent_association_respects_optional_conditions_on_delete
|
||||
firm = companies(:odegy)
|
||||
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
|
||||
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
|
||||
# only one of two clients is included in the association due to the :conditions key
|
||||
assert_equal 2, Client.find_all_by_client_of(firm.id).size
|
||||
assert_equal 1, firm.dependent_conditional_clients_of_firm.size
|
||||
firm.destroy
|
||||
# only the correctly associated client should have been deleted
|
||||
assert_equal 1, Client.find_all_by_client_of(firm.id).size
|
||||
end
|
||||
|
||||
def test_dependent_association_respects_optional_sanitized_conditions_on_delete
|
||||
firm = companies(:odegy)
|
||||
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
|
||||
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
|
||||
# only one of two clients is included in the association due to the :conditions key
|
||||
assert_equal 2, Client.find_all_by_client_of(firm.id).size
|
||||
assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
|
||||
firm.destroy
|
||||
# only the correctly associated client should have been deleted
|
||||
assert_equal 1, Client.find_all_by_client_of(firm.id).size
|
||||
end
|
||||
|
||||
def test_clearing_without_initial_access
|
||||
firm = companies(:first_firm)
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/company.rb
vendored
2
activerecord/test/fixtures/company.rb
vendored
@@ -50,6 +50,8 @@ end
|
||||
|
||||
class ExclusivelyDependentFirm < Company
|
||||
has_one :account, :foreign_key => "firm_id", :dependent => :delete
|
||||
has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
|
||||
has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
|
||||
end
|
||||
|
||||
class Client < Company
|
||||
|
||||
Reference in New Issue
Block a user