From 56acffee43c9417696e3cf34505fe07ac2e25ccf Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Sun, 27 Sep 2009 14:22:34 -0400 Subject: [PATCH] LH3230 - added #eager_loaded? to AR::Base, to identify eager loaded associations --- activerecord/lib/active_record/base.rb | 6 ++++++ activerecord/test/cases/base_test.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2ec2f73..8064ebb 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2454,6 +2454,12 @@ module ActiveRecord #:nodoc: end + # Returns true if association has been eager loaded; false otherwise. + # ex. model.eager_loaded?(:association) + def eager_loaded?(association) + !!self.instance_variable_get("@#{association}") + end + # Returns a String, which Action Pack uses for constructing an URL to this # object. The default implementation returns this record's id as a String, # or nil if this record's unsaved. diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 14f3967..cbf8b0b 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -731,6 +731,14 @@ class BasicsTest < ActiveRecord::TestCase assert !t.attribute_present?("content") end + def test_eager_loaded + a = Author.find(1, :include => :posts) + assert a.eager_loaded?(:posts) + + a = Author.find(1) + assert !(a.eager_loaded?(:posts)) + end + def test_attribute_keys_on_new_instance t = Topic.new assert_equal nil, t.title, "The topics table has a title column, so it should be nil" -- 1.6.0.2