# Default :include value for ActiveRecord::Base.find

**URL:** <https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104>\
**Category:** Rails\
**Created:** [May 14, 2007, 11:35am UTC](https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104 "2007-05-14T11:35:39Z")\
**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:** [May 14, 2007, 11:35am UTC](https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104/1 "2007-05-14T11:35:39Z")

</div>

Hi all,

I’m working on a application with lots of complicated data  
relationships and thousands of rows of data. To speed up queries, I’m  
using Project.find(:all, :conditions =\> some\_hash, :include =\>  
some\_other\_hash). I’m using the same hash for :include quite a lot -  
is there a way to specify in the Project model that it should use a  
certain :include hash in find() by default so I can DRY up my code?

Thanks,  
James

---

<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:** [May 14, 2007, 12:33pm UTC](https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104/2 "2007-05-14T12:33:48Z")

</div>

Hi

You can always define a class method for Project that wraps the find,  
something like (warn absolutely untested)

class Project  
def self.find\_std\_include(\*args)  
options = {:include =\> your\_standard\_include\_hash}  
if args.last.is\_a? Hash  
options.update(args.pop)  
end  
self.find(\*args.push(options))  
end  
end

This way you can call Project.find\_std\_include() every time you need  
to use the standard include clause.

Paolo

---

<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:** [May 14, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104/3 "2007-05-14T13:10:54Z")

</div>

Thanks Paolo. I figured a class method was probably the way to go, but  
was wondering if there was any way of modifying the default behaviour  
of find() on a per-class basis. Thanks for the help.

---

<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:** [May 14, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/default-include-value-for-activerecord-base-find/104104/4 "2007-05-14T13:31:29Z")

</div>

Sure, there is a way.

class Project  
class \<\< self  
alias find\_without\_standard\_includes find  
def find\_with\_standard\_includes(\*args)  
options = {:include =\> your\_standard\_include\_hash}  
if args.last.is\_a? Hash  
options.update(args.pop)  
end  
self.find\_without\_standard\_includes(\*args.push(options))  
end  
alias find find\_with\_standard\_includes  
end  
end

If you do not understand the class \<\< self syntax: Every Method inside  
class \<\< self … end is considered a method of the self object, that  
is the class itself in this context. This is needed, because alias does  
only work with method names (alias self.find\_without\_standard\_includes  
self.find does not work).

Have fun  
Florian

removed\_email\_address@domain.invalid wrote:

> Thanks Paolo. I figured a class method was probably the way to go, but  
> was wondering if there was any way of modifying the default behaviour  
> of find() on a per-class basis. Thanks for the help.
