This project is archived and is in readonly mode.

#5768 ✓stale
pupeno@pupeno.com

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

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

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?

Comments and changes to this ticket

  • pupeno@pupeno.com

    pupeno@pupeno.com October 8th, 2010 @ 06:09 AM

    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.

  • Jon Leighton

    Jon Leighton December 21st, 2010 @ 07:50 PM

    • 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

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages