Looking a thttps://github.com/rails/rails/blob/v3.1.0/activerecord/lib/active_record/relation.rb#L141-143
(and the same in v3.0.10, for example) I can't figure out why that
isn't the same, since the code is pretty much the same, so I'm
probably missing something.
That said, you have 3 ways of querying the size of an ActiveRecord relation:
* #length will load all the objects into an array (unless they are
already loaded) and then return the array's size.
* #count will force a DB count and return that result, without loading
the model instances into memory.
* #size will, if the objects are already loaded in an array, return
the #length, or otherwise call #count.
So you don't need to force an "all", that will load your objects in
memory. Just calling #count should do.
Cheers,
-foca
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
>
>
I can confirm what you have described: https://gist.github.com/1248588
In 3.0 there were inconsistencies between calling 'new' and 'build' on
an association. I consider this a bug. In 3.1 the inconsistencies are
tidied up, resulting in the behaviour you describe.
If you want to issue a COUNT query to the db, use #count rather than
#size.
Cheers