# Forcing load\_target

**URL:** <https://www.ruby-forum.com/t/forcing-load-target/182079>\
**Category:** Rails\
**Created:** [January 22, 2010, 11:02am UTC](https://www.ruby-forum.com/t/forcing-load-target/182079 "2010-01-22T11:02:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsynnott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rsynnott](https://www.ruby-forum.com/u/rsynnott)\
**Post date:** [January 22, 2010, 11:02am UTC](https://www.ruby-forum.com/t/forcing-load-target/182079/1 "2010-01-22T11:02:49Z")

</div>

Hi,  
I have the case where I have an object A, which has a has\_many through  
relationship with objects B. Each A will be associated with a  
relatively small number of Bs. I want to cycle through some Bs, and  
check if an A has them.

So, something like:  
a = A.find 123  
bs = B.all  
bs.map{|b| a.list\_of\_bs.include? b}

The problem is that in this case, while it would be more efficient to  
load all the Bs at once, ActiveRecord will, for each, do something  
like

SELECT Bs.id FROM Bs INNER JOIN As\_Bs ON Bs.id = As\_Bs.B\_id where  
Bs.id = 1 AND As\_Bs.B\_id = 123 LIMIT 1

This would make sense if I could have an arbitrary number of Bs  
associated with an A, but for my use case, loading all Bs for the A is  
more efficient. load\_target in association\_base does this, but is  
protected. Currently, I’m doing something like a.list\_of\_bs.length  
before everything else, which triggers load\_target, but this seems  
less than ideal. Is there a standard way to trigger load\_target?  
Thanks  
Rob

---

<div class="post-metadata">

**Author:** ![rsynnott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rsynnott](https://www.ruby-forum.com/u/rsynnott)\
**Post date:** [January 23, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/forcing-load-target/182079/2 "2010-01-23T16:47:55Z")

</div>

Hi,

I’m not sure I’m understanding. Are you asking about eager loading  
associations? If that is the case it can be done with :include when  
you get you A record.
