# AR Caching and Reflection

**URL:** <https://www.ruby-forum.com/t/ar-caching-and-reflection/61406>\
**Category:** Rails\
**Created:** [May 23, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406 "2006-05-23T21:51:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 23, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/1 "2006-05-23T21:51:21Z")

</div>

When I do this:  
term = Term.find(1, :include =\> :definition)  
term.definition.term  
Two SQL queries are fired, why?

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 23, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/2 "2006-05-23T22:49:07Z")

</div>

can you show the 2 queries ?

and did you make sure you defined the has\_X belongs\_to relationship in  
the  
model ?

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 24, 2006, 3:17am UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/3 "2006-05-24T03:17:12Z")

</div>

Adam D. wrote:

> can you show the 2 queries ?
> 
> and did you make sure you defined the has\_X belongs\_to relationship in  
> the  
> model ?

class Term \< ActiveRecord::Base  
has\_one :definition, :dependent =\> :destroy  
end

class Content \< ActiveRecord::Base  
end

class Definition \< Content  
belongs\_to :term  
end

I run this:

term = Term.find(2, :include =\> :definition)  
term.definition.term

Here are the SQL queries from the log (sorry for all the columns gumming  
up the join):

Term Load Including Associations (0.032398) SELECT terms.`id` AS  
t0\_r0, terms.`title` AS t0\_r1, terms.`created_at` AS t0\_r2,  
terms.`updated_at` AS t0\_r3, terms.`formatted_title` AS t0\_r4,  
terms.`definition_exists` AS t0\_r5, contents.`id` AS t1\_r0,  
contents.`article_id` AS t1\_r1, contents.`term_id` AS t1\_r2,  
contents.`parsed` AS t1\_r3, contents.`type` AS t1\_r4,  
contents.`parsed_at` AS t1\_r5, contents.`created_at` AS t1\_r6,  
contents.`updated_at` AS t1\_r7, contents.`author_id` AS t1\_r8 FROM terms  
LEFT OUTER JOIN contents ON contents.term\_id = terms.id AND  
contents.`type` = ‘Definition’ WHERE (terms.id = 2)

Term Load (0.000488) SELECT \* FROM terms WHERE (terms.id = 2) LIMIT  
1

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 24, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/4 "2006-05-24T22:01:43Z")

</div>

I’m sorry I was unclear. I was only using term.definition.term as a  
very basica example. What I’m actually trying to achieve is this:  
I grab a bunch of terms and include their definition. I process all of  
the terms I grabbed through various functions, some of which take a  
definition. In some of those definition handling functions the term is  
used. When those fire off they are in effect calling  
term.definition.term

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 24, 2006, 3:45pm UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/5 "2006-05-24T15:45:59Z")

</div>

i think you are looping here. Are you looking for the term based on a  
definition or the definition based on a term ?

you shoudl only need to do “term.definition” and that should give you  
the  
definition for the term id=2. Or does the definition table have an  
actual  
field called “term” ? Maybe thats what screwing this up here.

adam

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [May 25, 2006, 6:08am UTC](https://www.ruby-forum.com/t/ar-caching-and-reflection/61406/6 "2006-05-25T06:08:59Z")

</div>

On 5/24/06, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m sorry I was unclear. I was only using term.definition.term as a  
> very basica example. What I’m actually trying to achieve is this:  
> I grab a bunch of terms and include their definition. I process all of  
> the terms I grabbed through various functions, some of which take a  
> definition. In some of those definition handling functions the term is  
> used. When those fire off they are in effect calling  
> term.definition.term

# Term Load Including Associations

term = Term.find(1, :include =\> :definition)

# Term Load

term.definition.term

Even though it’s the same association, it’s not loaded automatically.  
One solution is to use cascading eager includes:

Term.find 1, :include =\> { :definition =\> :term }

But, I think this is very unnecessary in your case. Why not do this?

defs = Definition.find :all, :include =\> :term ?

# need to query on the terms?

Definition.find :all, :include =\> :term, :conditions =\> [‘terms.name =  
?’, ‘foo’]  
Definition.find :all, :include =\> :term, :conditions =\> [‘terms.id in  
(?)’, [1,2,3]]

# need to pull all the selected terms?

terms = defs.collect { |d| d.term }.uniq

It really helps to pull up the log and script/console so you can work  
with your models and see when the database is queried.
