Permalink
Browse files

Show the records in Relation#inspect

The reason for removing the previous implementation of `#inspect` was
that it hid from you that you were dealing with a `Relation` rather than
an `Array`.

But it is still useful to be able to see the records, particularly if you're
writing something like the following in tests:

    assert_equal [foo], Post.where(:bar)

If the assertion fails, you want to see what records were actually
loaded.

So this implementation makes it clear that you've got a `Relation`, but
also shows your records.
  • Loading branch information...
jonleighton committed Jul 6, 2012
1 parent 8ce61a3 commit 07314e64fd62fb8e6165c8c539420160da9437e9
View
@@ -20,14 +20,16 @@
*Aleksey Magusev*
-* `ActiveRelation#inspect` no longer calls `#to_a`. This means that in places
- where `#inspect` is implied (such as in the console), creating a relation
- will not execute it anymore, you'll have to call `#to_a` when necessary:
+* `ActiveRecord::Relation#inspect` now makes it clear that you are
+ dealing with a `Relation` object rather than an array:.
- User.where(:age => 30) # => returns the relation
- User.where(:age => 30).to_a # => executes the query and returns the loaded objects, as before
+ User.where(:age => 30).inspect
+ # => <ActiveRecord::Relation [#<User ...>, #<User ...>]>
- *Brian Cardarella*
+ User.where(:age => 30).to_a.inspect
+ # => [#<User ...>, #<User ...>]
+
+ *Brian Cardarella & Jon Leighton*
* Add `collation` and `ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
Example:
@@ -514,6 +514,10 @@ def values
@values.dup
end
+ def inspect
+ "#<#{self.class.name} #{to_a.inspect}>"
+ end
+
private
def references_eager_loaded_tables?
@@ -1311,4 +1311,9 @@ def test_presence
relation.merge! where: 'foo'
end
end
+
+ test "relations show the records in #inspect" do
+ relation = Post.limit(2)
+ assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
+ end
end

4 comments on commit 07314e6

@dmathieu

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Jul 6, 2012

Contributor

Actually, an other reason for removing inspect was that with a lot of records, it can freeze the console, or write a huge log.
This has apparently been a problem for some.

Contributor

dmathieu replied Jul 6, 2012

Actually, an other reason for removing inspect was that with a lot of records, it can freeze the console, or write a huge log.
This has apparently been a problem for some.

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Jul 6, 2012

Contributor

Agreed with @dmathieu. Maybe we can add a limit(11), then we show the first 10 and add ... in case 11 items were returned?

Contributor

josevalim replied Jul 6, 2012

Agreed with @dmathieu. Maybe we can add a limit(11), then we show the first 10 and add ... in case 11 items were returned?

@jonleighton

This comment has been minimized.

Show comment
Hide comment
@jonleighton

jonleighton Jul 6, 2012

Member

I like @josevalim's proposal. @dmathieu do you want to write a patch?

Member

jonleighton replied Jul 6, 2012

I like @josevalim's proposal. @dmathieu do you want to write a patch?

@dmathieu

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Jul 6, 2012

Contributor

Ok, I'll look into it this evening.

Contributor

dmathieu replied Jul 6, 2012

Ok, I'll look into it this evening.

Please sign in to comment.