Kevin Baker
Kevin Baker

Reputation: 1197

How do I force active record (Ruby) eager loading at the model level?

We would like to force our Post model to eager load all comments.

Right now we have to specify the eager loading at the find(:all), like below:

Post.all(:include => [ :comment ])

Is there a way to force an eager loading default at the Post model level, rather than having to do it in every find? Something like below:

class Post < ActiveRecord::Base
  has_many :comments, :include <all comments>  # eager load??

Upvotes: 6

Views: 1957

Answers (1)

girasquid
girasquid

Reputation: 15526

It looks like you'll want to tweak your default_scope for this.

Upvotes: 4

Related Questions