From 9a9f97af2815469e6f28dee9b88577251ef1b832 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 26 Dec 2009 15:28:23 +0530 Subject: Add relation.reload to force reloading the records --- activerecord/CHANGELOG | 8 ++++++++ activerecord/lib/active_record/relation.rb | 6 ++++++ activerecord/test/cases/relations_test.rb | 15 +++++++++++++++ 3 files changed, 29 insertions(+) (limited to 'activerecord') 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) -- cgit v1.2.3