Read more

Preload associations in loaded records

Arne Hartherz
September 07, 2010Software engineer at makandra GmbH

Sometimes you want to fetch associations for an ActiveRecord that you already loaded, e.g. when it has deeply nested associations.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

Edge Rider Show archive.org snapshot gives your models a static method preload_associations. The method can be used to preload associations for loaded objects like this:

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @user.preload_associations(threads: { posts: :author }, messages: :sender)
  end
end

The attached initializers remain for legacy purposes. Please prefer to use Edge Rider Show archive.org snapshot for new projects, which is being actively maintained and has test coverage for all versions of Rails.

Posted by Arne Hartherz to makandra dev (2010-09-07 10:44)