Preload associations in loaded records

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

Edge Rider Archive 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)

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

Arne Hartherz about 12 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more