Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2 (ActiveRecord) association order with eager loading and ordering
    text
    copied!<p>I came upon this behavior in rails that I can't seem to find in the documentation.</p> <p>It appears that if you complicate an ActiveRecord query to the point that it lumps the underlying SQL into a "complicated" query (see below) it doesn't honor ordering specified on the associations.</p> <p>Here's my example model:</p> <pre><code>class Article &lt; ActiveRecord::Base belongs_to :user has_many :categories, :order =&gt; :name attr_accessible :title, :user_id, :user end </code></pre> <p>If I perform a "simple" query, I can get the associated categories ordered by their name:</p> <pre><code>&gt; Article.includes(:categories).first.categories.map &amp;:name Article Load (0.1ms) SELECT "articles".* FROM "articles" LIMIT 1 Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."article_id" IN (11) ORDER BY name =&gt; ["category 3059", "category 3212", "category 3240", "category 3651", "category 4371", "category 5243", "category 6176", "category 6235", "category 6468", "category 654", "category 6804", "category 6892", "category 7026", "category 8929", "category 9653"] </code></pre> <p>You can see that the categories' names are in the expected order (note: I expect the 3 digit numbered categories to be in alphabetical order due to how the database orders strings).</p> <p>This next example is a more complicated query using another associations attribute for ordering:</p> <pre><code>&gt; Article.includes(:user, :categories).order('users.name').first.categories.map &amp;:name Article Load (0.3ms) SELECT DISTINCT "articles".id FROM "articles" LEFT OUTER JOIN "users" ON "users"."id" = "articles"."user_id" LEFT OUTER JOIN "categories" ON "categories"."article_id" = "articles"."id" ORDER BY users.name LIMIT 1 SQL (0.2ms) SELECT "articles"."id" AS t0_r0, "articles"."title" AS t0_r1, "articles"."user_id" AS t0_r2, "articles"."created_at" AS t0_r3, "articles"."updated_at" AS t0_r4, "users"."id" AS t1_r0, "users"."name" AS t1_r1, "users"."created_at" AS t1_r2, "users"."updated_at" AS t1_r3, "categories"."id" AS t2_r0, "categories"."name" AS t2_r1, "categories"."article_id" AS t2_r2, "categories"."created_at" AS t2_r3, "categories"."updated_at" AS t2_r4 FROM "articles" LEFT OUTER JOIN "users" ON "users"."id" = "articles"."user_id" LEFT OUTER JOIN "categories" ON "categories"."article_id" = "articles"."id" WHERE "articles"."id" IN (16) ORDER BY users.name =&gt; ["category 5023", "category 728", "category 3306", "category 8170", "category 5957", "category 7190", "category 4427", "category 3435", "category 1274", "category 7251", "category 7368", "category 682", "category 2918"] </code></pre> <p>As you can see, when AR lumps the query into one of its "complicated" SQL queries order is lost to the <code>categories</code> association.</p> <p>Is this behavior expected? Is it documented somewhere that I haven't been able to find?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload