Skip to content
This repository
Browse code

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
  • Loading branch information...
commit 437851ea02983e7f039de1a09a69ee32f7681134 1 parent 4efebde
Jon Leighton authored August 01, 2012
15  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,20 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add `Relation#load`
  4
+
  5
+    This method explicitly loads the records and then returns `self`.
  6
+
  7
+    Rather than deciding between "do I want an array or a relation?",
  8
+    most people are actually asking themselves "do I want to eager load
  9
+    or lazy load?" Therefore, this method provides a way to explicitly
  10
+    eager-load without having to switch from a `Relation` to an array.
  11
+
  12
+    Example:
  13
+
  14
+        @posts = Post.where(published: true).load
  15
+
  16
+    *Jon Leighton*
  17
+
3 18
 *   `:finder_sql` and `:counter_sql` options on collection associations
4 19
     are deprecated. Please transition to using scopes.
5 20
 
14  activerecord/lib/active_record/relation.rb
@@ -466,11 +466,21 @@ def delete(id_or_array)
466 466
       where(primary_key => id_or_array).delete_all
467 467
     end
468 468
 
  469
+    # Causes the records to be loaded from the database if they have not
  470
+    # been loaded already. You can use this if for some reason you need
  471
+    # to explicitly load some records before actually using them. The
  472
+    # return value is the relation itself, not the records.
  473
+    #
  474
+    #   Post.where(published: true).load # => #<ActiveRecord::Relation>
  475
+    def load
  476
+      to_a # force reload
  477
+      self
  478
+    end
  479
+
469 480
     # Forces reloading of relation.
470 481
     def reload
471 482
       reset
472  
-      to_a # force reload
473  
-      self
  483
+      load
474 484
     end
475 485
 
476 486
     def reset
8  activerecord/test/cases/relations_test.rb
@@ -1342,4 +1342,12 @@ def test_presence
1342 1342
     node = relation.arel.constraints.first.grep(Arel::Attributes::Attribute).first
1343 1343
     assert_equal table_alias, node.relation
1344 1344
   end
  1345
+
  1346
+  test '#load' do
  1347
+    relation = Post.all
  1348
+    assert_queries(1) do
  1349
+      assert_equal relation, relation.load
  1350
+    end
  1351
+    assert_no_queries { relation.to_a }
  1352
+  end
1345 1353
 end

2 notes on commit 437851e

Xavier Noria
Owner
fxn commented on 437851e August 01, 2012

Why not just alias load to_a? What is the point in not returning an array? I believe it should be easy and natural to get the an array of all users without a type-conversion style call.

Xavier Noria
Owner
fxn commented on 437851e August 02, 2012

Jon, we can talk about it in Basecamp if you want.

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