[Rails] Finding only the COUNT of a has_many association

I’m currently writing some software in which I just want to know the amount of has_many items a record has. I am under the impression that doing:


something.hasmanys.length

Will pull all the “hasmanys” from the database, so it would be a bog on the server if I were to do this? Or if I called this would it only call a COUNT() query? (which I am trying to do)

Check the logs ;-).

I don’t know about #length, but #size (and #count too) use a COUNT sql select.

From the docs:

Firm#clients.size (similar to Client.count “firm_id = #{id}”)

So:

something.hasmanys.size

Sorry for the late reply, I didn’t get a chance to really check this until today, but you were right! Thanks!

#count and #size indeed only use a count(*) query :slight_smile: Thanks