ruby on rails - Not being able to order by includes on Rails3 -
i have following expression:
user.clocks.includes(:users, :runs => :user_runs).find_by_id(params[:id])
which seems work fine. when add orders, this:
user.clocks.includes(:users, :runs => :user_runs).orders("users.names").find_by_id(params[:id])
it breaks following error:
activerecord::configurationerror: association named 'user_runs' not found; perhaps misspelled it? app/controllers/clocks_controller.rb:19:in `show' test/functional/clocks_controller_test.rb:21:in `__bind_1286475263_942556'
any ideas why?
the model looks this:
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 has_many :users, :through => :user_runs end class userrun < activerecord::base belongs_to :run belongs_to :user end
continuing investigation i've tried this:
ubiquitous_user.clocks.includes(:runs => :user_runs).find_by_id(params[:id])
and i've noticed queries it's generating doesn't user_runs @ all. odd.
i've created set of tests try figure going on:
context "a graph of users, clocks, runs, etc" setup @users = [] 10.times @users << factory.create(:user) end @clocks = [] 10.times @clocks << factory.create(:clock, :users => @users) end @clocks.each |clock| 10.times run = factory.create :run, :clock => clock @users.each |user| factory.create :user_run, :run => run, :user => user end end end @user = @users.first @clock = @clocks.first end should "find clock" assert_not_nil @user.clocks.find(@clock.id) end should "find clock users" assert_not_nil @user.clocks.includes(:users).find(@clock.id) end should "find clock users , runs" assert_not_nil @user.clocks.includes(:users, :runs).find(@clock.id) end should "find clock users, runs , user_runs" assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).find(@clock.id) end should "find clock users order users.name" assert_not_nil @user.clocks.includes(:users).order("users.name").find(@clock.id) end should "find clock users , runs order users.name" assert_not_nil @user.clocks.includes(:users, :runs).order("users.name").find(@clock.id) end should "find clock users, runs , user_runs order users.name" assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).order("users.name").find(@clock.id) end end
every test last 1 pass. not bug?
shouldn't be
user.clocks.find_by_id(params[:id], :include => [:users, {:runs => :user_runs}])
?
Comments
Post a Comment