1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
David Heinemeier Hansson 2004-12-17 21:36:13 +00:00
parent cf78e736d2
commit 648b8fda54
3 changed files with 38 additions and 7 deletions

View file

@ -1,3 +1,8 @@
*SVN*
* Added Base.destroy and Base.delete to remove records without holding a reference to them first.
*1.2.0* *1.2.0*
* Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable * Added Base.validates_inclusion_of that validates whether the value of the specified attribute is available in a particular enumerable

View file

@ -333,6 +333,16 @@ module ActiveRecord #:nodoc:
object object
end 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 # 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: # by specifying +conditions+. Example:
# Billing.update_all "category = 'authorized', approved = 1", "author = 'David'" # Billing.update_all "category = 'authorized', approved = 1", "author = 'David'"

View file

@ -26,9 +26,7 @@ end
class Booleantest < ActiveRecord::Base; end class Booleantest < ActiveRecord::Base; end
class BasicsTest < Test::Unit::TestCase class BasicsTest < Test::Unit::TestCase
def setup fixtures :topics, :companies
@topic_fixtures, @companies = create_fixtures "topics", "companies"
end
def test_set_attributes def test_set_attributes
topic = Topic.find(1) topic = Topic.find(1)
@ -36,7 +34,7 @@ class BasicsTest < Test::Unit::TestCase
topic.save topic.save
assert_equal("Budget", topic.title) assert_equal("Budget", topic.title)
assert_equal("Jason", topic.author_name) 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 end
def test_integers_as_nil def test_integers_as_nil
@ -187,14 +185,14 @@ class BasicsTest < Test::Unit::TestCase
def test_load def test_load
topics = Topic.find_all nil, "id" topics = Topic.find_all nil, "id"
assert_equal(2, topics.size) assert_equal(2, topics.size)
assert_equal(@topic_fixtures["first"]["title"], topics.first.title) assert_equal(@topics["first"]["title"], topics.first.title)
end end
def test_load_with_condition def test_load_with_condition
topics = Topic.find_all "author_name = 'Mary'" topics = Topic.find_all "author_name = 'Mary'"
assert_equal(1, topics.size) assert_equal(1, topics.size)
assert_equal(@topic_fixtures["second"]["title"], topics.first.title) assert_equal(@topics["second"]["title"], topics.first.title)
end end
def test_table_name_guesses def test_table_name_guesses
@ -556,4 +554,22 @@ class BasicsTest < Test::Unit::TestCase
topic = Topic.create('content' => content) topic = Topic.create('content' => content)
assert_equal content, Topic.find(topic.id).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 end