aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorFrederick Cheung <frederick.cheung@gmail.com>2008-12-21 15:38:40 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-12-21 15:57:48 +0000
commitb17b9371c6a26484eb1984d45acffcdcd91b1ae1 (patch)
treef81c502a724e8383faf90d6b28250fd54860e9e5 /activerecord
parent6f4b2469fb19bb01fa0f53192eb49f8f2d95db1b (diff)
downloadrails-b17b9371c6a26484eb1984d45acffcdcd91b1ae1.tar.gz
rails-b17b9371c6a26484eb1984d45acffcdcd91b1ae1.tar.bz2
rails-b17b9371c6a26484eb1984d45acffcdcd91b1ae1.zip
Fix configure_dependency_for_has_many not quoting conditions properly [#1461 state:resolved]
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb6
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb13
-rw-r--r--activerecord/test/models/company.rb1
3 files changed, 17 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 07bc50c886..5a60b13fd8 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1453,7 +1453,7 @@ module ActiveRecord
dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
dependent_conditions << extra_conditions if extra_conditions
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
-
+ dependent_conditions = dependent_conditions.gsub('@', '\@')
case reflection.options[:dependent]
when :destroy
method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym
@@ -1467,7 +1467,7 @@ module ActiveRecord
delete_all_has_many_dependencies(record,
"#{reflection.name}",
#{reflection.class_name},
- "#{dependent_conditions}")
+ %@#{dependent_conditions}@)
end
}
when :nullify
@@ -1477,7 +1477,7 @@ module ActiveRecord
"#{reflection.name}",
#{reflection.class_name},
"#{reflection.primary_key_name}",
- "#{dependent_conditions}")
+ %@#{dependent_conditions}@)
end
}
else
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 816ceb6855..20b9acda44 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -665,6 +665,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, Client.find_all_by_client_of(firm.id).size
end
+ def test_dependent_association_respects_optional_hash_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_creation_respects_hash_condition
ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb
index 0e3fafa37c..3b27a9e272 100644
--- a/activerecord/test/models/company.rb
+++ b/activerecord/test/models/company.rb
@@ -80,6 +80,7 @@ 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.']
+ has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'}
end
class Client < Company