aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-09-28 03:52:57 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-09-28 03:52:57 +0000
commitefaf2af07c4238e911899988871e575d8ef5805b (patch)
treea70ca8cfb227cb6aa47ac68fea84641fc4bab28d /activerecord/lib/active_record
parent1e87d6e887639650e23db5fbba0ccdf0536f289c (diff)
downloadrails-efaf2af07c4238e911899988871e575d8ef5805b.tar.gz
rails-efaf2af07c4238e911899988871e575d8ef5805b.tar.bz2
rails-efaf2af07c4238e911899988871e575d8ef5805b.zip
r3653@asus: jeremy | 2005-09-28 00:23:49 -0700
Ticket 2221 - model.association.clear should destroy associated objects if :dependent => true instead of nullifying their foreign keys git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2384 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb8
2 files changed, 10 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index b2a2ed6e11..dce4f350e8 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -227,7 +227,9 @@ module ActiveRecord
# This will also destroy the objects if they're declared as belongs_to and dependent on this model.
# * <tt>collection=objects</tt> - replaces the collections content by deleting and adding objects as appropriate.
# * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+
- # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects.
+ # * <tt>collection.clear</tt> - removes every object from the collection. This destroys the associated objects if they
+ # are <tt>:dependent</tt>, deletes them directly from the database if they are <tt>:exclusively_dependent</tt>,
+ # and sets their foreign keys to NULL otherwise.
# * <tt>collection.empty?</tt> - returns true if there are no associated objects.
# * <tt>collection.size</tt> - returns the number of associated objects.
# * <tt>collection.find</tt> - finds an associated object according to the same rules as Base.find.
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index f9ca655560..2c881290aa 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -87,7 +87,13 @@ module ActiveRecord
# Removes all records from this association. Returns +self+ so
# method calls may be chained.
def clear
- @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}")
+ 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