Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is rails console limiting the query output?

I'm running rails in cloud9 IDE. I'm running a query in the rails console which should just give me all the Users in a user table I have created. This has 100 records However when I run:

User.all

I get

 => #<ActiveRecord::Relation [#<User id: 1, name: "Expedita Eaque", group: 1>, #<User id: 2, name: "Omnis Vel Tempora", group: 3>, #<User id: 3, name: "Incidunt", group: 1>, #<User id: 4, name: "Aliquam", group: 3>, #<User id: 5, name: "Dolorum", group: 1>, #<User id: 6, name: "At", group: 2>, #<User id: 7, name: "Dignissimos", group: 1>, #<User id: 8, name: "Eligendi Amet Ut", group: 1>, #<User id: 9, name: "Corporis Sint", group: 5>, #<User id: 10, name: "Quis Explicabo", group: 1>, ...]> 

which is clearly not 100 records. How can I get rails console to show me all the records instead of truncating with , ...]> ?

like image 797
Sebastian Zeki Avatar asked Jun 10 '15 07:06

Sebastian Zeki


People also ask

What does Rails console do?

The console command lets you interact with your Rails application from the command line. On the underside, bin/rails console uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.


1 Answers

User.all Returns an ActiveRecord::Relation object, not the actual results.

To see all the records, you can try

User.all.to_a

This behaviour is purposeful and is created so for performance reasons. You can read more about lazy loading in ORMs to better understand why.

Basically, you need to call a method on the AR relation object which actually needs the underlying data, only then is the query formed and executed.

EDIT

The console works a little differently to your application, in as much as, for any object that's returned in the console, it does an inspection and writes out the truncated output. So basically, it does force the query to run on its own, but shows limited results for viewing ease. To actually see all results, use a method that will force execution of the query and will return the full result set.

like image 176
vvohra87 Avatar answered Sep 24 '22 11:09

vvohra87