# Has\_many :through eager loading only loading one record

**URL:** <https://www.ruby-forum.com/t/has-many-through-eager-loading-only-loading-one-record/183476>\
**Category:** Rails\
**Created:** [February 15, 2010, 9:48am UTC](https://www.ruby-forum.com/t/has-many-through-eager-loading-only-loading-one-record/183476 "2010-02-15T09:48:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@Jarrett](https://www.ruby-forum.com/u/Jarrett)\
**Post date:** [February 15, 2010, 9:48am UTC](https://www.ruby-forum.com/t/has-many-through-eager-loading-only-loading-one-record/183476/1 "2010-02-15T09:48:23Z")

</div>

Hello,

I’ve been using Rails for years, including has\_many :through and eager  
loading, but this one has me stumped.

The problem is that Active Record is only eager-loading the first of  
many records. Here are the models (note set\_table\_name for a legacy  
schema):

class Tag \< ActiveRecord::Base  
set\_table\_name ‘tag’  
end

class DishTag \< Tag  
has\_many :dish\_taggings, :foreign\_key =\> ‘tag\_id’  
has\_many :dishes, :through =\> :dish\_taggings  
end

class DishTagging \< ActiveRecord::Base  
set\_table\_name ‘dish\_tag’  
belongs\_to :dish  
belongs\_to :dish\_tag, :foreign\_key =\> ‘tag\_id’  
end

class Dish \< ActiveRecord::Base  
set\_table\_name ‘dish’  
has\_many :dish\_taggings, :foreign\_key =\> ‘tag\_id’  
has\_many :tags, :through =\> :dish\_taggings, :source =\> :dish\_tag  
end

In the controller, I eager-load like this:

@tags = DishTag.find(:all, :include =\> {:dish\_taggings =\> :dish})

Here are the resulting queries (with traces thanks to the query\_trace  
plugin):

DishTag Columns (1.2ms) SHOW FIELDS FROM `tag`  
DishTag Load (0.9ms) SELECT \* FROM `tag` WHERE ( (`tag`.`type` =  
‘DishTag’ ) )  
app/controllers/tags\_controller.rb:5:in `index' DishTagging Load (6.9ms) SELECT`dish\_tag`.* FROM`dish\_tag`WHERE (`dish\_tag`.tag_id IN (58,60,61,62,63,79,81,82,83,90,91,93,94,96,97,99,102,104,106,109,110,114,120,121,122,123,124,125,126,127,131,132,133,135,136,137,138,139,141,143,144,145,146,147,149,150,153,154,157,158,161,166,178,181,184,199,229,232,235,238,259,262,277,283,298,301,304,307,310,313,322,331,334,337,340,343,346,352,355,358,361,364,370,373,376,379,391,394,400,403,406,409,412,415,418,436,439,442,445,448,451,454,457,481,484,532,535,547,550,553,556,559,562,568,580,706,721,724,730,733,736,739,742,745,748,751,754,757,760,763,766,769,778,781,784,787,790,793,796,799,802,805,808,811,814,817,820,823,826,829,832,835,838,841,844,847,850,853,856,859,862,865,868,871,874,877,880,883,886,889,892,895,898,901,904,907,910,913,916,919,922,925,928,934,937,940,943,946,949,952,955,958,961,964,967,970,973,979,982,985,988,991,994,1003,1006,1009,1012,1015,1018,1021,1024,1027,1030,1033,1036,1039,1042,1045,1048,1051,1054,1057,1060,1063,1066,1069,1072,1075,1078,1081,1084,1087)) app/controllers/tags_controller.rb:5:in`index’  
DishTagging Columns (1.2ms) SHOW FIELDS FROM `dish_tag`  
Dish Columns (2.0ms) SHOW FIELDS FROM `dish`  
Dish Load (0.4ms) SELECT \* FROM `dish` WHERE (`dish`.`id` = 209)  
app/controllers/tags\_controller.rb:5:in `index’

Notice that in DishTagging Load, the first foreign key in the list is  
58. As it turns out, there is only one DishTagging witth tag\_id 58,  
and its dish\_id is 209. Now look at Dish Load: it’s loading dish 209  
and nothing else.

So, this leads me to believe that only the first related record is  
being eager-loaded. Sure enough, when I iterate, Active Record loads  
each dish individually except for dish 209.

Any thoughts on why this is happening? I’ve never seen this problem  
before. I’m guessing it has something to do with all the stuff I’ve  
had to override in the model configs (e.g. table names and foreign  
keys.) But I’m not getting any exceptions.

---

<div class="post-metadata">

**Author:** ![Jarrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@Jarrett](https://www.ruby-forum.com/u/Jarrett)\
**Post date:** [February 15, 2010, 9:50am UTC](https://www.ruby-forum.com/t/has-many-through-eager-loading-only-loading-one-record/183476/2 "2010-02-15T09:50:44Z")

</div>

On Feb 14, 11:24 pm, Jarrett [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Notice that in DishTagging Load, the first foreign key in the list is  
> 58. As it turns out, there is only one DishTagging witth tag\_id 58,  
> and its dish\_id is 209. Now look at Dish Load: it’s loading dish 209  
> and nothing else.

Dumb question: Are their dish\_tags with tag\_id 60,61,62 etc. What is  
their dish\_id ?

Fred

---

<div class="post-metadata">

**Author:** ![Jarrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@Jarrett](https://www.ruby-forum.com/u/Jarrett)\
**Post date:** [February 15, 2010, 9:53am UTC](https://www.ruby-forum.com/t/has-many-through-eager-loading-only-loading-one-record/183476/3 "2010-02-15T09:53:39Z")

</div>

On Feb 15, 8:49 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> their dish\_id ?

Another thought - you seem to have some sort of STI thing going on  
(DishTag \< Tag). Does the type column on the tags column contain  
appropriate values?

Fred
