The method to_a is not an intuitive way to force ActiveRecord to not re-query the db when accessing a collection of child objects

Or perhaps aliasing to_a with something like run_query

There’s load, which does approximately that.

Specifically, load is the operation that various other things, including to_a, trigger when they actually need the records to be present. Unlike to_a, load returns self, so you still have a Relation that answers to all the extra methods; to_a follows the Ruby convention and converts the vaguely-array-ish Relation collection object into a totally standard Ruby Array.

(Some of these things may be less true for Rails 3.x, which I see you mentioned above… but I don’t think there’s much we can do to retroactively improve old versions.)

2 Likes