# How to force AR belongs\_to include option to eager load?

**URL:** <https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330>\
**Category:** Rails\
**Created:** [November 2, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330 "2009-11-02T23:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 2, 2009, 11:56pm UTC](https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330/1 "2009-11-02T23:56:28Z")

</div>

Hello

I have two classes

class SolarSystem \< ActiveRecord::Base  
has\_many :fow\_ss\_entries, :dependent =\> :delete\_all

class FowSsEntry \< ActiveRecord::Base  
belongs\_to :galaxy  
belongs\_to :solar\_system  
belongs\_to :player

named\_scope :for, Proc.new { |galaxy, player|  
galaxy = galaxy.id if galaxy.is\_a? Galaxy  
player = player.id if player.is\_a? Player

```
{
  :conditions => {:galaxy_id => galaxy, :player_id => player},
  :include => :solar_system
}

```

}

Now the evil thing is, that even thou’ :include is specified, it does  
nothing. New AR eager loading resorts to doing n+1 sql queries in  
FowSsEntry.for(1,1).each { |fse| fse.solar\_system }

How do I force it into joining my table, without impacting my SQL  
performance?

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 3, 2009, 1:20am UTC](https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330/2 "2009-11-03T01:20:48Z")

</div>

On Nov 2, 10:56 pm, Artûras Ðlajus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Now the evil thing is, that even thou’ :include is specified, it does  
> nothing. New AR eager loading resorts to doing n+1 sql queries in  
> FowSsEntry.for(1,1).each { |fse| fse.solar\_system }
> 
> How do I force it into joining my table, without impacting my SQL  
> performance?

Is the issue the fact that :include does not always trigger a join  
based strategy ? What are the actual sql queries you see?

Fred

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 3, 2009, 11:09am UTC](https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330/3 "2009-11-03T11:09:14Z")

</div>

Frederick C. wrote:

> On Nov 2, 10:56ï¿½pm, Artï¿½ras ï¿½lajus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Now the evil thing is, that even thou’ :include is specified, it does  
> > nothing. New AR eager loading resorts to doing n+1 sql queries in  
> > FowSsEntry.for(1,1).each { |fse| fse.solar\_system }
> > 
> > How do I force it into joining my table, without impacting my SQL  
> > performance?
> 
> Is the issue the fact that :include does not always trigger a join  
> based strategy ? What are the actual sql queries you see?  
> Yes.

I see this:  
[2009-11-03 00:35:26|main|debug] FowSsEntry Load (0.0ms) SELECT \* FROM  
`fow_ss_entries` WHERE (`fow_ss_entries`.`galaxy_id` = 1 AND  
`fow_ss_entries`.`player_id` = 1)  
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT \* FROM  
`solar_systems` WHERE (`solar_systems`.`id` = 57)  
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT \* FROM  
`solar_systems` WHERE (`solar_systems`.`id` = 157)  
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT \* FROM  
`solar_systems` WHERE (`solar_systems`.`id` = 313)  
[2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT \* FROM  
`solar_systems` WHERE (`solar_systems`.`id` = 314)

Which is kind of bad ;(

---

<div class="post-metadata">

**Author:** ![arturaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/73ab20/32.png) [@arturaz](https://www.ruby-forum.com/u/arturaz)\
**Post date:** [November 11, 2009, 12:43am UTC](https://www.ruby-forum.com/t/how-to-force-ar-belongs-to-include-option-to-eager-load/178330/4 "2009-11-11T00:43:06Z")

</div>

Is there a method for forcing a join-based strategy?

On Nov 3, 2:09 am, Artûras Ðlajus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
