Robert Robert - 3 years ago 64
Ruby Question

Is the object that comes back as a result of a database query, an array or hash?

Was wondering what kind of form an object comes back when a query is returned from the database.

For example:

@Users = User.all


I'm thinking that when the all method queries all the users from the database, it comes back as an array since the each method is used eventually in the view.

But when I take a look in the console, the returned object seems like a hash but has the square brackets so I was wondering what the object comes back as.

How the console displays the returned object:

[#<User id: 1, email: "example@gmail.com"...>]>

Answer Source

http://guides.rubyonrails.org/active_record_basics.html

It comes back as what rails calls an active record relation. It has most of the methods available to an array available to it, and has access to specific methods that retrieve records from the database far faster than standard queries, see 'where'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download