How to eager load association for an array of model records
Well, you could refind those objects. Map ':id' over your array, to get the records' ids, and then refind, this time eager-loading.
If your array of, say, Post
model records is posts
, then it'd be:
Post.find(posts.map &:id).includes(:blah)
In Rails 3, one can use preloader to eager-load associations on existing records.
ActiveRecord::Associations::Preloader.new(posts,:comments).run()
In Rails 4.1+ the call signature changed slightly:
ActiveRecord::Associations::Preloader.new.preload(posts,:comments)