I frequently run into this situation.
post_comments = post.comments
#grabs maybe 3 records out of thousands in the table
do_something_with post_comments
#...later in the same request...
subset_of_comments = post_comments.where(:awesome=>true)
#hits the database again :-(
do_something_else subset_of_comments
I realize that the database is very good at finding records, but I don’t see how going back to the full table and looking through thousands of records is better than searching the small set of records attached to this post that’s already cached for the 1 or two I need.
What could I do if I wanted to improve the efficiency of this process?
You can use the select method:
Documentation Array (Ruby 1.9.3),
#selectmethod