public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix configure_dependency_for_has_many not quoting conditions properly [#1461 
state:resolved]
fcheung (author)
Sun Dec 21 07:38:40 -0800 2008
lifo (committer)
Sun Dec 21 07:57:48 -0800 2008
commit  b17b9371c6a26484eb1984d45acffcdcd91b1ae1
tree    f81c502a724e8383faf90d6b28250fd54860e9e5
parent  6f4b2469fb19bb01fa0f53192eb49f8f2d95db1b
...
1453
1454
1455
1456
 
1457
1458
1459
...
1467
1468
1469
1470
 
1471
1472
1473
...
1477
1478
1479
1480
 
1481
1482
1483
...
1453
1454
1455
 
1456
1457
1458
1459
...
1467
1468
1469
 
1470
1471
1472
1473
...
1477
1478
1479
 
1480
1481
1482
1483
0
@@ -1453,7 +1453,7 @@ module ActiveRecord
0
             dependent_conditions << sanitize_sql(reflection.options[:conditions]) if reflection.options[:conditions]
0
             dependent_conditions << extra_conditions if extra_conditions
0
             dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
0
-
0
+            dependent_conditions = dependent_conditions.gsub('@', '\@')
0
             case reflection.options[:dependent]
0
               when :destroy
0
                 method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym
0
@@ -1467,7 +1467,7 @@ module ActiveRecord
0
                     delete_all_has_many_dependencies(record,
0
                       "#{reflection.name}",
0
                       #{reflection.class_name},
0
-                      "#{dependent_conditions}")
0
+                      %@#{dependent_conditions}@)
0
                   end
0
                 }
0
               when :nullify
0
@@ -1477,7 +1477,7 @@ module ActiveRecord
0
                       "#{reflection.name}",
0
                       #{reflection.class_name},
0
                       "#{reflection.primary_key_name}",
0
-                      "#{dependent_conditions}")
0
+                      %@#{dependent_conditions}@)
0
                   end
0
                 }
0
               else
...
665
666
667
 
 
 
 
 
 
 
 
 
 
 
 
 
668
669
670
...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
0
@@ -665,6 +665,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
0
     assert_equal 1, Client.find_all_by_client_of(firm.id).size
0
   end
0
 
0
+  def test_dependent_association_respects_optional_hash_conditions_on_delete
0
+    firm = companies(:odegy)
0
+    Client.create(:client_of => firm.id, :name => "BigShot Inc.")
0
+    Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
0
+    # only one of two clients is included in the association due to the :conditions key
0
+    assert_equal 2, Client.find_all_by_client_of(firm.id).size
0
+    assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
0
+    firm.destroy
0
+    # only the correctly associated client should have been deleted
0
+    assert_equal 1, Client.find_all_by_client_of(firm.id).size
0
+  end
0
+
0
+
0
   def test_creation_respects_hash_condition
0
     ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
0
 
...
80
81
82
 
83
84
85
...
80
81
82
83
84
85
86
0
@@ -80,6 +80,7 @@ class ExclusivelyDependentFirm < Company
0
   has_one :account, :foreign_key => "firm_id", :dependent => :delete
0
   has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
0
   has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
0
+  has_many :dependent_hash_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => {:name => 'BigShot Inc.'}
0
 end
0
 
0
 class Client < Company

Comments