From 437851ea02983e7f039de1a09a69ee32f7681134 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 1 Aug 2012 21:42:38 +0100 Subject: Add `Relation#load` This method explicitly loads the records and then returns `self`. Rather than deciding between "do I want an array or a relation?", most people are actually asking themselves "do I want to eager load or lazy load?" Therefore, this method provides a way to explicitly eager-load without having to switch from a `Relation` to an array. Example: @posts = Post.where(published: true).load --- activerecord/lib/active_record/relation.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/relation.rb') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 3821c6122a..a8170dc489 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -466,11 +466,21 @@ module ActiveRecord where(primary_key => id_or_array).delete_all end + # Causes the records to be loaded from the database if they have not + # been loaded already. You can use this if for some reason you need + # to explicitly load some records before actually using them. The + # return value is the relation itself, not the records. + # + # Post.where(published: true).load # => # + def load + to_a # force reload + self + end + # Forces reloading of relation. def reload reset - to_a # force reload - self + load end def reset -- cgit v1.2.3