The difference between the two strategies for intensive loading is discussed here in the comments.
https://github.com/rails/rails/blob/3-0-stable/activerecord/lib/active_record/association_preload.rb
From the documentation:
# The second strategy is to use multiple database queries, one for each
I believe that the dot in "foo.bar" makes the active record think that you are placing a condition in a table that is outside the original model, which requests the second strategy discussed in the documentation.
Two separate requests start one with the Person model, and the second with the Item model.
Person.includes(:items).where(:name => 'fubar') Person Load (0.2ms) SELECT "people".* FROM "people" WHERE "people"."name" = 'fubar' Item Load (0.4ms) SELECT "items".* FROM "items" WHERE ("items".person_id = 1) ORDER BY items.ordinal
Since you run the second query for the Item model, it inherits the default scope in which you specified order(:ordinal) .
The second request, which he tries to load with the full launch of the user model and will not use the default communication area.
Person.includes(:items).where(:name => 'foo.bar') Person Load (0.4ms) SELECT "people"."id" AS t0_r0, "people"."name" AS t0_r1, "people"."created_at" AS t0_r2, "people"."updated_at" AS t0_r3, "items"."id" AS t1_r0, "items"."person_id" AS t1_r1, "items"."name" AS t1_r2, "items"."ordinal" AS t1_r3, "items"."created_at" AS t1_r4, "items"."updated_at" AS t1_r5 FROM "people" LEFT OUTER JOIN "items" ON "items"."person_id" = "people"."id" WHERE "people"."name" = 'foo.bar'
Think about it a bit, but I see how it would be with several different ways that you can present a list of options to make sure that you catch them all, would scan the completed βWHEREβ conditions for the point and use the second strategy, and they leave her that way because both strategies are functional. In fact, I would say that aberrant behavior is in the first request, and not in the second. If you want the order to be saved for this request, I recommend one of the following:
1) If you want a link to have an order when it is called, you can specify this using an association. Oddly enough, this is in the documentation, but I could not get it to work.
Source: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
class Person < ActiveRecord::Base has_many :items, :order => 'items.ordinal' end
2) Another method would be to simply add an order operator to the request in question.
Person.includes(:items).where(:name => 'foo.bar').order('items.ordinal')
3) A named area will be created along the same lines
class Person < ActiveRecord::Base has_many :items named_scope :with_items, includes(:items).order('items.ordinal') end
And to call it:
Person.with_items.where(:name => 'foo.bar')