Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

add method to check if association is eager loaded

#3239

To optimize sql queries for an application, it is helpful to see if associations that will be used are eager loaded. A simple method will allow users to easily check this. We implemented a version that would be helpful for other users.

class ActiveRecord::Base
  # Returns true if association has been eager loaded; false otherwise.
  # ex. model.eager_loaded?(:association)
  def eager_loaded?(association)
    !!self.instance_variable_get("@#{association}")
  end
end

Reported by 2 College Bums · September 20th, 2009 @ 09:50 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from active_record, association, associations, associations_preload to active_record, association, associations, associations_preload, bugmash

    September 25th, 2009 @ 11:56 AM

  2. Elad Meidar
    Elad Meidar

    +1 might come useful around tests, more than in a production environment. i've attached patches for 2-3-stable and master.

    September 27th, 2009 @ 07:31 PM

  3. Elad Meidar
    Elad Meidar
    • Tag changed from active_record, association, associations, associations_preload, bugmash to active_record, association, associations, associations_preload, bugmash-review

    September 27th, 2009 @ 07:34 PM

  4. Michael Koziarski
    Michael Koziarski
    • State changed from new to wontfix
    • Tag changed from active_record, association, associations, associations_preload, bugmash-review to active_record, association, associations, associations_preload

    We already have a method for determining whether a collection was loaded

    foo.bars.loaded?

    Seems this is a subset of that?

    September 28th, 2009 @ 02:44 AM