Permalink
Browse files

Added Base#reload that reloads the attributes of an object from the d…

…atabase #422 [Andreas Schwarz]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@376 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 59c2a4d commit 3ff5c5805fb490591b5839db8445869a48e93bcb @dhh dhh committed Jan 10, 2005
Showing with 20 additions and 2 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +8 −1 activerecord/lib/active_record/base.rb
  3. +10 −1 activerecord/test/base_test.rb
View
@@ -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]
@@ -874,6 +874,13 @@ def toggle!(attribute)
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 <tt>attr_name</tt> 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 @@ def has_yaml_encoding_header?(string)
string[0..3] == "--- "
end
end
-end
+end
@@ -620,4 +620,13 @@ def test_toggle_attribute
@topics["first"].find.toggle!(:approved)
assert @topics["first"].find.approved?
end
-end
+
+ 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

0 comments on commit 3ff5c58

Please sign in to comment.