#577 ✓resolved
Phil Darnowsky

Model.all(:links => [:foo, :bar]), the first time it's run, does not initialize the foo and bar associations

Reported by Phil Darnowsky | September 26th, 2008 @ 02:22 AM

This problem seems to arise only the first time the Model.all query is run, so I speculate that it may depend on having a clean identity map.

Run the attached join_models_setup.rb, then join_models.rb. Observe these lines from the end of the output of join_models.rb:


Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") ORDER BY "join_models"."id"
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") WHERE ("join_models"."id" IN (1)) ORDER BY "join_models"."id"
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") WHERE ("join_models"."id" IN (1)) ORDER BY "join_models"."id"
[#<JoinModel id=1 model_a_id=nil model_b_id=nil>]
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") WHERE ("join_models"."id" IN (1)) ORDER BY "join_models"."id"
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") WHERE ("join_models"."id" IN (1)) ORDER BY "join_models"."id"
Model A: nil, Model B: nil
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "join_models"."id", "join_models"."model_a_id", "join_models"."model_b_id" FROM "join_models" INNER JOIN "model_as" ON ("model_as"."id" = "join_models"."model_a_id") INNER JOIN "model_bs" ON ("model_bs"."id" = "join_models"."model_b_id") ORDER BY "join_models"."id"
[#<JoinModel id=1 model_a_id=1 model_b_id=1>]
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "id" FROM "model_as" WHERE ("id" IN (1)) ORDER BY "id"
Fri, 26 Sep 2008 07:20:50 GMT ~ debug ~ SELECT "id" FROM "model_bs" WHERE ("id" IN (1)) ORDER BY "id"
Model A: #<ModelA id=1>, Model B: #<ModelB id=1>

Observe the last 7 lines of the output. Note that both SQL queries execute the join properly, but only the second one actually selects the join columns as well, and thus only the second one populates the association properly.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Attachments

Pages