From 5fa8793f02b58509a90eed69e8dc3d199f89b4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Fri, 14 Oct 2005 12:53:39 +0000 Subject: DRYed up Associations#clear. Closes #1906 [Caleb] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2580 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations/association_collection.rb | 17 +++++++++++++++++ .../has_and_belongs_to_many_association.rb | 20 -------------------- .../associations/has_many_association.rb | 14 -------------- 3 files changed, 17 insertions(+), 34 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 932e8e2aae..ced734cfcd 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -33,6 +33,12 @@ module ActiveRecord alias_method :push, :<< alias_method :concat, :<< + + # Remove all records from this association + def delete_all + delete(@target) + @target = [] + end # Remove +records+ from this association. Does not destroy +records+. def delete(*records) @@ -50,6 +56,17 @@ module ActiveRecord end end end + + # Removes all records from this association. Returns +self+ so method calls may be chained. + def clear + return self if empty? # forces load_target if hasn't happened already + if @options[:exclusively_dependent] + destroy_all + else + delete_all + end + self + end def destroy_all @owner.transaction do diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index d386a5a906..04774fa28a 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -19,26 +19,6 @@ module ActiveRecord record end - # Removes all records from this association. Returns +self+ so method calls may be chained. - def clear - return self if size == 0 # forces load_target if hasn't happened already - - if sql = @options[:delete_sql] - each { |record| @owner.connection.execute(sql) } - elsif @options[:conditions] - sql = - "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = #{@owner.quoted_id} " + - "AND #{@association_foreign_key} IN (#{collect { |record| record.id }.join(", ")})" - @owner.connection.execute(sql) - else - sql = "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = #{@owner.quoted_id}" - @owner.connection.execute(sql) - end - - @target = [] - self - end - def find_first load_target.first end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 2c881290aa..114f84ed5d 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -83,20 +83,6 @@ module ActiveRecord @association_class.find(*args) end end - - # Removes all records from this association. Returns +self+ so - # method calls may be chained. - def clear - if @options[:dependent] - each { |associate| associate.destroy } - elsif @options[:exclusively_dependent] - @association_class.delete_all("#{@association_class_primary_key_name} = #{@owner.quoted_id}") - else - @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}") - end - @target = [] - self - end protected def find_target -- cgit v1.2.3