# AR.find :include is adding stuff I don't want

**URL:** <https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999>\
**Category:** Rails\
**Created:** [April 8, 2009, 6:35am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999 "2009-04-08T06:35:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 6:35am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/1 "2009-04-08T06:35:48Z")

</div>

In this code below, ActiveRecord returns data from associations I don’t  
want.

class Article \< ActiveRecord::Base

has\_many :extras  
has\_many :pages, :order =\> ‘sortOrder’  
has\_many :assets

def get\_list  
return self.find(:all,  
:include =\> :extras,  
:conditions =\> [“publishable=‘Y’”])  
end  
end

Without the :include statement, we get the usual lazy load, and no  
associations are loaded, only the attributes of the Article.

However, I want data from the :extras association and not the others.

When I use the code above, I get data from Article (good), Extras  
(good), and Assets (bad), and nothing from Pages (curious).

I don’t understand why Assets is being included, and I don’t know how to  
keep that data out of the results.

What am I missing? Thx.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 9:19am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/2 "2009-04-08T09:19:05Z")

</div>

On Apr 8, 5:35 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When I use the code above, I get data from Article (good), Extras  
> (good), and Assets (bad), and nothing from Pages (curious).
> 
> I don’t understand why Assets is being included, and I don’t know how to  
> keep that data out of the results.

Odd. Maybe you could show the sql it is executing ?

Fred

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 10:52am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/3 "2009-04-08T10:52:47Z")

</div>

Frederick C. wrote:

> On Apr 8, 5:35ï¿½am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > When I use the code above, I get data from Article (good), Extras  
> > (good), and Assets (bad), and nothing from Pages (curious).
> > 
> > I don’t understand why Assets is being included, and I don’t know how to  
> > keep that data out of the results.
> 
> Odd. Maybe you could show the sql it is executing ?

Hmm. now I am seeing something different.

With this code (more verbatim this time)…

class Article \< ActiveRecord::Base

self.primary\_key = ‘rcrd\_id’

has\_many :extras,  
:dependent =\> :destroy

has\_many :pages,  
:order =\> ‘sortOrder’,  
:dependent =\> :destroy

has\_many :assets,  
:dependent =\> :destroy

def self.get\_list  
list = self.find(:all,  
:include =\> [:extras],  
:conditions =\> [  
“#{self.table\_name}.rcrd\_status=‘Y’ AND ready=‘Y’ AND  
publishDate \<= :today”,  
{:today =\> Date.today.to\_s} ])  
end  
end

Or with this code…

def self.get\_list  
list = self.find(:all,  
:conditions =\> [  
“#{self.table\_name}.rcrd\_status=‘Y’ AND ready=‘Y’ AND publishDate  
\<= :today”,  
{:today =\> Date.today.to\_s} ])  
end

I now see these same queries in the log…  
(and this is restarting Rails after each time I change the code, just to  
be sure)

e[4;36;1mArticle Load (1.6ms)e[0m e[0;1mSELECT \* FROM `articles`  
WHERE (articles.rcrd\_status=‘Y’ AND ready=‘Y’ AND publishDate \<=  
‘2009-04-08’) e[0m  
e[4;35;1mAsset Load (0.8ms)e[0m e[0mSELECT \* FROM `assets` WHERE  
(`assets`.article\_id = ‘abc123’) e[0m  
e[4;36;1mExtra Load (1.1ms)e[0m e[0;1mSELECT \* FROM `extras` WHERE  
(`extras`.article\_id = ‘abc123’) e[0m

I know it was different before, I even showed someone to confirm it.

I don’t see anything odd about the other models, they both have a simple  
belongs\_to articles statement, and nothing else. Pages has a belongs\_to  
articles, and a has\_many to another model. Maybe that keeps pages from  
being loaded?

So, I dunno. But shouldn’t that second finder case (without :include)  
not be returning :assets nor :extras? I end up using debug() to output  
the results to confirm what is actually being loaded, and I get those  
two associations, but no :pages data. Seems weird.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 11:06am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/4 "2009-04-08T11:06:28Z")

</div>

On Apr 8, 9:52 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> > On Apr 8, 5:35 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > wrote:

> So, I dunno. But shouldn’t that second finder case (without :include)  
> not be returning :assets nor :extras? I end up using debug() to output  
> the results to confirm what is actually being loaded, and I get those  
> two associations, but no :pages data. Seems weird.

Random question: are you doing this from the console or is this output  
from an app. Are you sure the load is coming from the find and not  
from the association been accessed subsequently (eg put a log  
statement after the find(…, :include) ? )

Fred

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 11:52am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/5 "2009-04-08T11:52:14Z")

</div>

On 8 Apr 2009, at 10:44, Greg W. wrote:

> wasn’t  
> remembering after\_find.
> 
> Egads. So sorry to waste your time Fred. But proving there was no log  
> statement question pushed me to at least solve the (not so puzzling)  
> puzzle. Thanks.

D’oh! Happens to all of us 🙂

Fred

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [April 8, 2009, 11:44am UTC](https://www.ruby-forum.com/t/ar-find-include-is-adding-stuff-i-dont-want/164999/6 "2009-04-08T11:44:57Z")

</div>

Frederick C. wrote:

> On Apr 8, 9:52ï¿½am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Frederick C. wrote:
> > 
> > > On Apr 8, 5:35 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > wrote:
> 
> > So, I dunno. But shouldn’t that second finder case (without :include)  
> > not be returning :assets nor :extras? I end up using debug() to output  
> > the results to confirm what is actually being loaded, and I get those  
> > two associations, but no :pages data. Seems weird.
> 
> Random question: are you doing this from the console or is this output  
> from an app. Are you sure the load is coming from the find and not  
> from the association been accessed subsequently (eg put a log  
> statement after the find(…, :include) ? )

@#$#! After writing a big long thing to prove there was no other action  
taking place, and taking one more wider look before sending it to be  
sure I wasn’t making an idiot of myself, I just scrolled down a little  
further in the model to re-discover an after\_find I wrote which would in  
fact touch assets & extras.

I had just so compartmentalized the :include thing in my mind, I wasn’t  
remembering after\_find.

Egads. So sorry to waste your time Fred. But proving there was no log  
statement question pushed me to at least solve the (not so puzzling)  
puzzle. Thanks.

– gw
