提交 de0ea386 编写于 作者: P Pratik Naik

Ensure :dependent => :delete_all works for association with hash conditions

上级 489abfd3
...@@ -1375,7 +1375,7 @@ def configure_dependency_for_has_many(reflection, extra_conditions = nil) ...@@ -1375,7 +1375,7 @@ def configure_dependency_for_has_many(reflection, extra_conditions = nil)
dependent_conditions = [] dependent_conditions = []
dependent_conditions << "#{reflection.primary_key_name} = \#{record.quoted_id}" dependent_conditions << "#{reflection.primary_key_name} = \#{record.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]) 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
dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ") dependent_conditions = dependent_conditions.collect {|where| "(#{where})" }.join(" AND ")
dependent_conditions = dependent_conditions.gsub('@', '\@') dependent_conditions = dependent_conditions.gsub('@', '\@')
......
...@@ -719,6 +719,12 @@ def test_dependence ...@@ -719,6 +719,12 @@ def test_dependence
assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty? assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
end end
def test_dependence_for_associations_with_hash_condition
david = authors(:david)
post = posts(:thinking).id
assert_difference('Post.count', -1) { assert david.destroy }
end
def test_destroy_dependent_when_deleted_from_association def test_destroy_dependent_when_deleted_from_association
firm = Firm.find(:first) firm = Firm.find(:first)
assert_equal 2, firm.clients.size assert_equal 2, firm.clients.size
......
...@@ -25,7 +25,7 @@ def testing_proxy_target ...@@ -25,7 +25,7 @@ def testing_proxy_target
has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'" has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' } has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' }, :dependent => :delete_all
has_many :welcome_posts, :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' } has_many :welcome_posts, :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' }
has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC' has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
......
...@@ -78,13 +78,6 @@ class DependentFirm < Company ...@@ -78,13 +78,6 @@ class DependentFirm < Company
has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
end 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.']
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 class Client < Company
belongs_to :firm, :foreign_key => "client_of" belongs_to :firm, :foreign_key => "client_of"
belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id" belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
...@@ -125,6 +118,12 @@ def private_method ...@@ -125,6 +118,12 @@ def private_method
end end
end 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.']
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 SpecialClient < Client class SpecialClient < Client
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册