Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Base.destroy and Base.delete to remove records without holding …

…a reference to them first.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@206 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 648b8fda54e5de742e4ffbb4bc8e6edf0b68a111 1 parent cf78e73
@dhh dhh authored
View
5 activerecord/CHANGELOG
@@ -1,3 +1,8 @@
+*SVN*
+
+* Added Base.destroy and Base.delete to remove records without holding a reference to them first.
+
+
*1.2.0*
* Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable
View
10 activerecord/lib/active_record/base.rb
@@ -333,6 +333,16 @@ def update(id, attributes)
object
end
+ # Deletes the record with the given +id+ without instantiating an object first.
+ def delete(id)
+ delete_all([ "#{primary_key} = ?", id ])
+ end
+
+ # Destroys the record with the given +id+ by instantiating the object and calling #destroy (all the callbacks are the triggered).
+ def destroy(id)
+ find(id).destroy
+ end
+
# Updates all records with the SET-part of an SQL update statement in +updates+. A subset of the records can be selected
# by specifying +conditions+. Example:
# Billing.update_all "category = 'authorized', approved = 1", "author = 'David'"
View
30 activerecord/test/base_test.rb
@@ -26,9 +26,7 @@ class TightDescendent < TightPerson
class Booleantest < ActiveRecord::Base; end
class BasicsTest < Test::Unit::TestCase
- def setup
- @topic_fixtures, @companies = create_fixtures "topics", "companies"
- end
+ fixtures :topics, :companies
def test_set_attributes
topic = Topic.find(1)
@@ -36,7 +34,7 @@ def test_set_attributes
topic.save
assert_equal("Budget", topic.title)
assert_equal("Jason", topic.author_name)
- assert_equal(@topic_fixtures["first"]["author_email_address"], Topic.find(1).author_email_address)
+ assert_equal(@topics["first"]["author_email_address"], Topic.find(1).author_email_address)
end
def test_integers_as_nil
@@ -187,14 +185,14 @@ def test_initialize_with_attributes
def test_load
topics = Topic.find_all nil, "id"
assert_equal(2, topics.size)
- assert_equal(@topic_fixtures["first"]["title"], topics.first.title)
+ assert_equal(@topics["first"]["title"], topics.first.title)
end
def test_load_with_condition
topics = Topic.find_all "author_name = 'Mary'"
assert_equal(1, topics.size)
- assert_equal(@topic_fixtures["second"]["title"], topics.first.title)
+ assert_equal(@topics["second"]["title"], topics.first.title)
end
def test_table_name_guesses
@@ -556,4 +554,22 @@ def test_quote
topic = Topic.create('content' => content)
assert_equal content, Topic.find(topic.id).content
end
-end
+
+ def test_class_level_destroy
+ should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
+ @first.replies << should_be_destroyed_reply
+
+ Topic.destroy(1)
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) }
+ assert_raises(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
+ end
+
+ def test_class_level_delete
+ should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
+ @first.replies << should_be_destroyed_reply
+
+ Topic.delete(1)
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) }
+ assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.