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_tagWHERE (
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.