Preload associations in rails
Preloading associations is well known, include used to do it
Post.include(:user, :comments).limit(10)
What if you need to run it on already fetched collection? ActiveRecord::Associations::Preloader helps.
ActiveRecord::Associations::Preloader.new( collectionsofobjects, associations )
Where collectionsofobjects is array of ActiveRecord objects and associations is specifies one or more associations that you want to preload. Same syntax as used in include (Here more info) [http://apidock.com/rails/ActiveRecord/Associations/Preloader/new/class]
Usage:
posts = Post.limit(10) #… ActiveRecord::Associations::Preloader.new( posts, [ :user, :comments, { :location => [:city, :country]} ] ).run










