Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

A set of includes in active records break when I add an order

#5768

I'll give you more context information bellow, but first, let me show you some active records call.

First a simple "let's get a clock":

    @user.clocks.find(@clock.id)

that works. Then, let's include some associations

    @user.clocks.includes(:users).find(@clock.id)
    @user.clocks.includes(:users, :runs).find(@clock.id)
    @user.clocks.includes(:users, :runs => :user_runs).find(@clock.id)

all right. Now let's order them:

    @user.clocks.includes(:users).order("users.name").find(@clock.id)
    @user.clocks.includes(:users, :runs).order("users.name").find(@clock.id)
    @user.clocks.includes(:users, :runs => :user_runs).order("users.name").find(@clock.id)

all work except the last one. The last one throws this error:

    ActiveRecord::ConfigurationError: Association named 'user_runs' was not found; perhaps you misspelled it?

but note that the same includes in the previous set of queries, without the order, worked fine (although I think it was not retrieving :user_runs).

Interestingly enough, if I try to run it with the old syntax:

   @user.clocks.find(@clock.id, :include => [:users, {:runs => :user_runs}], :order => "users.name")

it doesn't throw an exception.

Is this not a bug? am I missing something?

Reported by pupeno@pupeno.com · October 8th, 2010 @ 06:05 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. pupeno@pupeno.com
    pupeno@pupeno.com

    This is the relevant context:

    class User < ActiveRecord::Base
      has_and_belongs_to_many :clocks
      has_many :runs, :through => :clocks
      has_many :user_runs
      #...
    end
    
    class Clock < ActiveRecord::Base
      has_and_belongs_to_many :users
      has_many :runs
      # ...
    end
    
    class Run < ActiveRecord::Base
      belongs_to :clock
      has_many :user_runs
      # ...
    end
    
    class UserRun < ActiveRecord::Base
      belongs_to :run
      belongs_to :user
      # ...
    end
    

    and

      create_table "clocks", :force => true do |t|
      end
    
      create_table "clocks_users", :id => false, :force => true do |t|
        t.integer "clock_id", :null => false
        t.integer "user_id",  :null => false
      end
    
      create_table "runs", :force => true do |t|
        t.integer  "clock_id"
      end
    
      create_table "user_runs", :force => true do |t|
        t.integer  "run_id"
        t.integer  "user_id"
      end
    
      create_table "users", :force => true do |t|
      end
    

    Thanks.

    October 8th, 2010 @ 06:09 AM

  2. Jon Leighton
    Jon Leighton
    • State changed from new to stale
    • Importance changed from to Low

    Hi there,

    Thanks for the bug report.

    I've tried out your code against Rails 3.0.3 and cannot reproduce the problem.

    I'm marking as stale, let me know if the problem does still exist and I can re-open.

    Jon

    December 21st, 2010 @ 07:50 PM