power power - 3 months ago 11x
Ruby Question

Difference between ActiveRecord and ActiveRecord::Relation objects

I have searched but not able to find the brief explanation for the difference between ActiveRecord and ActiveRecord::relation object.

I have understand that ActiveRecord is the single object find by something like


And ActiveRecord::Relation is the array like object Find by something like

User.where(id: 1)

I am looking for the difference between them in terms of query execution or deep explanation about them, so it will clear the whole concept behind it.

Thanks in advance!


An instance of ActiveRecord::Base is an object that represents a specific row of your database (or might be saved into the database).

Whereas a instance of ActiveRecord::Relation is a representation a query that can be run against your database (but wasn't run yet). Once you run that query by calling to_a, each, first etc. on that Relation a single instance or an array of ActiveRecord::Base instances is returned.