The two Active Record queries: User.first.followers.count
and User.first.followers.to_a.count
evaluate to the following SQL queries respectively:
[4] pry(main)> User.first.followers.count
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
(0.3ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ? [["followed_id", 1]]
=> 4
[5] pry(main)> User.first.followers.to_a.count
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ? [["followed_id", 1]]
=> 4
How are they conceptually different?
This question came up in an exercise question at the end of section 14.1 in The Rails Tutorial
He gives the following hint:
Hint: Suppose that the user had a million followers.