Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jun 13 ’11

Check if your associations have been eager loaded

If you want to check if associations of an object have been eager loaded, use the loaded? method:

@blogpost = Post.includes(:comments).find(1)
@blogpost.comments.loaded?
=> true

@blogpost = Post.find(1)
@blogpost.comments.loaded?
=> false

This might for example be useful to test eager loading in your tests.

4 notes 0 comments

  1. nicktj reblogged this from rubyquicktips and added:
    there’s performance
  2. atm09td reblogged this from rubyquicktips
  3. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus