Skip to content

Commit

Permalink
Add Relation#load
Browse files Browse the repository at this point in the history
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
jonleighton committed Aug 1, 2012
1 parent 4efebde commit 437851e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
15 changes: 15 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,20 @@
## Rails 4.0.0 (unreleased) ##

* 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

*Jon Leighton*

* `:finder_sql` and `:counter_sql` options on collection associations
are deprecated. Please transition to using scopes.

Expand Down
14 changes: 12 additions & 2 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -466,11 +466,21 @@ def delete(id_or_array)
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 # => #<ActiveRecord::Relation>
def load
to_a # force reload
self
end

# Forces reloading of relation.
def reload
reset
to_a # force reload
self
load
end

def reset
Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -1342,4 +1342,12 @@ def test_presence
node = relation.arel.constraints.first.grep(Arel::Attributes::Attribute).first
assert_equal table_alias, node.relation
end

test '#load' do
relation = Post.all
assert_queries(1) do
assert_equal relation, relation.load
end
assert_no_queries { relation.to_a }
end
end

2 comments on commit 437851e

@fxn
Copy link
Member

@fxn fxn commented on 437851e Aug 1, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fxn
Copy link
Member

@fxn fxn commented on 437851e Aug 2, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.