length will load all your objects just to count them; something like:
select * from addresses...
and then return the results count.
As you can imagine - it's bad performance
count will just issue
select count(*) from addresses...
which is better, because we are not loading all addresses just to count them
size is smarter - it'll check if the association is already loaded and if true then return the length (without issuing a call to the database).
size also checks for counter_cache if you have a field named address_count in your user model, then size will use this field for the count, so there is no need to issue a count on the addresses table.
if all fails, size will issue a select count(*)
on the database
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…