# Eager loading w/ :through question

**URL:** <https://www.ruby-forum.com/t/eager-loading-w-through-question/111502>\
**Category:** Rails\
**Created:** [August 8, 2007, 12:16am UTC](https://www.ruby-forum.com/t/eager-loading-w-through-question/111502 "2007-08-08T00:16:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![licinius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@licinius](https://www.ruby-forum.com/u/licinius)\
**Post date:** [August 8, 2007, 12:16am UTC](https://www.ruby-forum.com/t/eager-loading-w-through-question/111502/1 "2007-08-08T00:16:57Z")

</div>

I’m doing an absolutely insane amount of queries, so I starting looking  
into how to fix it, because there isn’t really a reason to, as I can get  
the relevant data with just one.

Essentially it looks like this. I have a list of training criteria  
called “awarenesses”, that have a certain amount of relevancy to a given  
“position”, the “positionawarenesses” is the join object that defines  
this relationship and has a field that says how important it is called  
“level” (along with an “awareness\_id” and “position\_id” of course.

This query gives me a table with everything I should need… including  
the “level”

```
@awarenesses = Awareness.find(:all, :include =>

```

[:positionawarenesses])

I’m trying to avoid having to do (which makes another query):

```
@awarenesses[0].positionawarenesses.find_by_position_id(12).level

```

How do I get to that information using the result of the first bit  
there?

---

<div class="post-metadata">

**Author:** ![licinius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@licinius](https://www.ruby-forum.com/u/licinius)\
**Post date:** [August 8, 2007, 1:01am UTC](https://www.ruby-forum.com/t/eager-loading-w-through-question/111502/2 "2007-08-08T01:01:20Z")

</div>

Not sure if that’s what you’re trying to achieve but

@awarenesses[0].positionawarenesses.select { |pa| pa.position\_id == 12  
}.first.level

might do the job.

Paolo

---

<div class="post-metadata">

**Author:** ![licinius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@licinius](https://www.ruby-forum.com/u/licinius)\
**Post date:** [August 8, 2007, 1:46am UTC](https://www.ruby-forum.com/t/eager-loading-w-through-question/111502/3 "2007-08-08T01:46:54Z")

</div>

Paolo N. wrote:

> @awarenesses[0].positionawarenesses.select { |pa| pa.position\_id == 12  
> }.first.level

This definitely gets the value I’m looking for, which cut my queries in  
half, yay! Not sure about the efficiency as I’m using that some 2000  
times in a loop? Alas the values are different every time.

---

<div class="post-metadata">

**Author:** ![licinius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@licinius](https://www.ruby-forum.com/u/licinius)\
**Post date:** [August 8, 2007, 2:28am UTC](https://www.ruby-forum.com/t/eager-loading-w-through-question/111502/4 "2007-08-08T02:28:31Z")

</div>

Given that positionawarenesses has its own properties, you could also  
pull this out into its own model. If nothing else, this would create a  
very efficient query.

class PositionAwareness \< ActiveRecord::Base  
:belongs\_to :awareness  
:belongs\_to :position

```
def self.find_by_position_id_and_awareness_id(position_id,

```

awareness\_id)  
find(  
:all,  
:conditions =\> [  
“position\_id = ? and awareness\_id = ?”,  
position\_id,  
awareness\_id  
]  
)  
end  
end

And in your controller:  
PositionAwareness.find\_by\_position\_id\_and\_awareness\_id(12,  
@awarenesses[0].id)

Chris

On Aug 8, 9:46 am, Brett B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
