# Always eager load

**URL:** <https://www.ruby-forum.com/t/always-eager-load/85908>\
**Category:** Rails\
**Created:** [February 17, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/always-eager-load/85908 "2007-02-17T15:16:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 17, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/always-eager-load/85908/1 "2007-02-17T15:16:32Z")

</div>

Is it possible to make a model always eager load an association,  
without explicitly specifying it every time? For example:

class Article \< AR::Base  
has\_one :author  
end

I’m _always_ going to need the Author for the Article, so is there a  
way to set :include =\> :author by default for find()? I thought about  
overwriting find() on Article and then just call super() with  
the :include, but I’m not exactly sure how that would go. Any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 17, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/always-eager-load/85908/2 "2007-02-17T17:15:46Z")

</div>

This seems to work:

def self.find(\*args)  
options = extract\_options\_from\_args!(args)  
options[:include] ||= :author  
super(args.first, options)  
end

Is there an easier way or are there any problems with this approach?

Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 17, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/always-eager-load/85908/3 "2007-02-17T17:28:47Z")

</div>

If there’s already an options[:include] you’re going to use it then,  
right?  
[That’s what the code says.] If you’re wanting to append it to any other  
manual :include, you’ll need to convert options[:include] to an array  
and  
all. If there’s no other possible models to include then you could  
options[:include] = :author instead. But I’d say you’ve got it solved  
already.

RSL

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2007, 5:49am UTC](https://www.ruby-forum.com/t/always-eager-load/85908/4 "2007-02-18T05:49:58Z")

</div>

Actually, it’s the opposite, if there is an :include defined then it  
does nothing, this way I can specify :include =\> nil if I didn’t want  
to eager load (which won’t happen, but just in case). That seems to do  
the job then, thank you.
