aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG8
-rw-r--r--activerecord/lib/active_record/relation.rb6
-rw-r--r--activerecord/test/cases/relations_test.rb15
3 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index d13a9e61df..e584979251 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,13 @@
*Edge*
+* Add relation.reload to force reloading the records. [Pratik Naik]
+
+ topics = Topic.scoped
+ topics.to_a # force load
+ topics.first # returns a cached record
+ topics.reload
+ topics.first # Fetches a new record from the database
+
* Rename Model.conditions and relation.conditions to .where. [Pratik Naik]
Before :
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index a030ba29fa..853103a606 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -120,6 +120,12 @@ module ActiveRecord
@loaded
end
+ def reload
+ @loaded = false
+ @records = @first = nil
+ self
+ end
+
private
def method_missing(method, *args, &block)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 475090fbd8..2b3c6eec1d 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -55,6 +55,21 @@ class RelationTest < ActiveRecord::TestCase
assert topics.loaded?
end
+ def test_reload
+ topics = Topic.scoped
+
+ assert_queries(1) do
+ 2.times { topics.to_a }
+ end
+
+ assert topics.loaded?
+
+ topics.reload
+ assert ! topics.loaded?
+
+ assert_queries(1) { topics.to_a }
+ end
+
def test_finding_with_conditions
assert_equal ["David"], Author.where(:name => 'David').map(&:name)
assert_equal ['Mary'], Author.where(["name = ?", 'Mary']).map(&:name)