diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-20 18:28:45 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-20 18:28:45 +0530 |
commit | 97568056763dc3bca7804a1426fae32a2031cfec (patch) | |
tree | e680792167faceb74018651ac6d78eb73bda36aa /activerecord/lib/active_record/relation.rb | |
parent | 223e2a2709cbd0013d51b024bb4e0f950586c125 (diff) | |
download | rails-97568056763dc3bca7804a1426fae32a2031cfec.tar.gz rails-97568056763dc3bca7804a1426fae32a2031cfec.tar.bz2 rails-97568056763dc3bca7804a1426fae32a2031cfec.zip |
Move destroy to Relation
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 217fbb2ff4..a253dbd19d 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -119,6 +119,33 @@ module ActiveRecord end end + # Destroy an object (or multiple objects) that has the given id, the object is instantiated first, + # therefore all callbacks and filters are fired off before the object is deleted. This method is + # less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run. + # + # This essentially finds the object (or multiple objects) with the given id, creates a new object + # from the attributes, and then calls destroy on it. + # + # ==== Parameters + # + # * +id+ - Can be either an Integer or an Array of Integers. + # + # ==== Examples + # + # # Destroy a single object + # Todo.destroy(1) + # + # # Destroy multiple objects + # todos = [1,2,3] + # Todo.destroy(todos) + def destroy(id) + if id.is_a?(Array) + id.map { |one_id| destroy(one_id) } + else + find(id).destroy + end + end + def delete_all arel.delete.tap { reset } end |