diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-12-26 15:28:23 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-12-26 15:31:50 +0530 |
commit | 9a9f97af2815469e6f28dee9b88577251ef1b832 (patch) | |
tree | d6a581b91deba8b91ace9ff55d13a0fc6f1b618b /activerecord | |
parent | 3c5a7dcaf55f427f3a48e206feb06410d011ca4f (diff) | |
download | rails-9a9f97af2815469e6f28dee9b88577251ef1b832.tar.gz rails-9a9f97af2815469e6f28dee9b88577251ef1b832.tar.bz2 rails-9a9f97af2815469e6f28dee9b88577251ef1b832.zip |
Add relation.reload to force reloading the records
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 15 |
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) |