From 3ff5c5805fb490591b5839db8445869a48e93bcb Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 10 Jan 2005 23:49:57 +0000 Subject: Added Base#reload that reloads the attributes of an object from the database #422 [Andreas Schwarz] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@376 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 9 ++++++++- activerecord/test/base_test.rb | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8bc77eca5b..46870fb3a5 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added Base#reload that reloads the attributes of an object from the database #422 [Andreas Schwarz] + * Added SQLite3 compatibility through the sqlite3-ruby adapter by Jamis Buck #381 [bitsweat] * Added support for the new protocol spoken by MySQL 4.1.1+ servers for the Ruby/MySQL adapter that ships with Rails #440 [Matt Mower] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index bd7195a33b..8be5c7302a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -874,6 +874,13 @@ module ActiveRecord #:nodoc: toggle(attribute).update_attribute(attribute, self[attribute]) end + # Reloads the attributes of this object from the database. + def reload + clear_association_cache + @attributes.update(self.class.find(self.id).instance_variable_get('@attributes')) + return self + end + # Returns the value of attribute identified by attr_name after it has been type cast (for example, # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). # (Alias for the protected read_attribute method). @@ -1211,4 +1218,4 @@ module ActiveRecord #:nodoc: string[0..3] == "--- " end end -end \ No newline at end of file +end diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index ccd9152e60..119be8d868 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -620,4 +620,13 @@ class BasicsTest < Test::Unit::TestCase @topics["first"].find.toggle!(:approved) assert @topics["first"].find.approved? end -end \ No newline at end of file + + def test_reload + t1 = Topic.find(1) + t2 = Topic.find(1) + t1.title = "something else" + t1.save + t2.reload + assert_equal t1.title, t2.title + end +end -- cgit v1.2.3