3 ms·
what would you use ?
by drum 13y ago
what would you use ?
- ovechtrick 13y agoUser.count would do it.
- kaffeinecoma 13y agoYou should use "User.count", which will do something like "select count(*) from users". "Users.all.count" will needlessly read in all users into memory and then return a count. That might work OK for 1000 users, but beyond that?
- AznHisoka 13y agoYup. But even that takes a long time to run in Postgres if you got millions of rows. I usually run User.last.id - User.first.id, and that usually gives me the # of rows (give or take)
- chaz 13y agoIn Rails 4, User.count and User.all.count both do the same thing: SELECT COUNT(*) FROM "users" In earlier versions, User.all.count executes: SELECT "users".* FROM "users" because it is pulling back all fields from all users into memory, instead of just asking the database to check the index for a count.
- meowface 13y agoThis is an easy mix-up to make considering .length, .count, and .size are all aliases.
- rajsinghtut 13y agoNewb question here - Did you use Rails as your backend? How does it fit into your stack as an iOS app? Are you using something like RubyMotion? Just curious...