Skip to content
This repository
Browse code

Add relation.reload to force reloading the records

  • Loading branch information...
commit 9a9f97af2815469e6f28dee9b88577251ef1b832 1 parent 3c5a7dc
Pratik authored December 26, 2009
8  activerecord/CHANGELOG
... ...
@@ -1,5 +1,13 @@
1 1
 *Edge*
2 2
 
  3
+* Add relation.reload to force reloading the records. [Pratik Naik]
  4
+
  5
+  topics = Topic.scoped
  6
+  topics.to_a  # force load
  7
+  topics.first # returns a cached record
  8
+  topics.reload
  9
+  topics.first # Fetches a new record from the database
  10
+
3 11
 * Rename Model.conditions and relation.conditions to .where. [Pratik Naik]
4 12
 
5 13
   Before :
6  activerecord/lib/active_record/relation.rb
@@ -120,6 +120,12 @@ def loaded?
120 120
       @loaded
121 121
     end
122 122
 
  123
+    def reload
  124
+      @loaded = false
  125
+      @records = @first = nil
  126
+      self
  127
+    end
  128
+
123 129
     private
124 130
 
125 131
     def method_missing(method, *args, &block)
15  activerecord/test/cases/relations_test.rb
@@ -55,6 +55,21 @@ def test_loaded_first
55 55
     assert topics.loaded?
56 56
   end
57 57
 
  58
+  def test_reload
  59
+    topics = Topic.scoped
  60
+
  61
+    assert_queries(1) do
  62
+      2.times { topics.to_a }
  63
+    end
  64
+
  65
+    assert topics.loaded?
  66
+
  67
+    topics.reload
  68
+    assert ! topics.loaded?
  69
+
  70
+    assert_queries(1) { topics.to_a }
  71
+  end
  72
+
58 73
   def test_finding_with_conditions
59 74
     assert_equal ["David"], Author.where(:name => 'David').map(&:name)
60 75
     assert_equal ['Mary'],  Author.where(["name = ?", 'Mary']).map(&:name)

0 notes on commit 9a9f97a

Please sign in to comment.
Something went wrong with that request. Please try again.