12

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

User.find(1)

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!

power
  • 1,225
  • 7
  • 16

3 Answers3

20

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

Whereas, an instance of ActiveRecord::Relation is a representation of 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 will be returned.

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • thanks @spickermann, when I try on rails console then it shows me sql query is get executed in case of active record relation, but it should not be because it is a representation. Can you please explain about it? – power Jul 12 '16 at 10:29
  • 2
    @power : It gets executed in the console, because the console calls `inspect` on that relation to output the result. If you change the line to not return the relation (like `User.where(id: 10); nil`), you will see that the query is not run. Whereas `User.find(10); nil` would still run the query. – spickermann Jul 12 '16 at 10:34
  • 1
    Is it too simplistic to say: `ActiveRecord::Base` represents a row from the database, and `ActiveRecord::Relation` represent an sql query that will query a database? So this is the fundamental difference between FinderMethods and QueryMethods, perhaps? – J.R. Bob Dobbs Nov 13 '22 at 23:15
3

Rails uses activerecord as standard ORM but the same applies for activerecord on its own.

In short: all queries that yield multiple records like scopes, all, where, and joins return an ActiveRecord::Relation object. You can chain these together, and it is only when you use a method like to_sql, first, each, any, to_a, take, last etc that the query is executed and returns an array of ActiveRecord::Base instead


OuttaSpaceTime
  • 710
  • 10
  • 24
peter
  • 41,770
  • 5
  • 64
  • 108
1

when you use record by find method and that record not present in the database than you will get below error

User.find(10)


User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 10]] ActiveRecord::RecordNotFound: Couldn't find User with 'id'=10

and if you find user by where condition and if user not present in database than you will get nill record like below

User.where(id: 10)


User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ?  [["id", 10]]
 => #<ActiveRecord::Relation []> 

It will not give any error

Vishal
  • 7,113
  • 6
  • 31
  • 61