我有以下表达式:
user.clocks.includes(:users, :runs => :user_runs).find_by_id(params[:id])
似乎工作正常。但是当我添加订单时,就像这样:
user.clocks.includes(:users, :runs => :user_runs).orders("users.names").find_by_id(params[:id])
它因以下错误而中断:
ActiveRecord::ConfigurationError: Association named 'user_runs' was not found; perhaps you misspelled it?
app/controllers/clocks_controller.rb:19:in `show'
test/functional/clocks_controller_test.rb:21:in `__bind_1286475263_942556'
任何想法为什么?
该模型如下所示:
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
继续我的调查我试过这个:
ubiquitous_user.clocks.includes(:runs => :user_runs).find_by_id(params[:id])
我注意到它生成的查询根本没有得到user_runs。有点奇怪。
我已经创建了一组测试来试图弄清楚发生了什么:
context "A graph of users, clocks, runs, etc" do
setup do
@users = []
10.times do
@users << Factory.create(:user)
end
@clocks = []
10.times do
@clocks << Factory.create(:clock, :users => @users)
end
@clocks.each do |clock|
10.times do
run = Factory.create :run, :clock => clock
@users.each do |user|
Factory.create :user_run, :run => run, :user => user
end
end
end
@user = @users.first
@clock = @clocks.first
end
should "find a clock" do
assert_not_nil @user.clocks.find(@clock.id)
end
should "find a clock with users" do
assert_not_nil @user.clocks.includes(:users).find(@clock.id)
end
should "find a clock with users and runs" do
assert_not_nil @user.clocks.includes(:users, :runs).find(@clock.id)
end
should "find a clock with users, runs and user_runs" do
assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).find(@clock.id)
end
should "find a clock with users order by users.name" do
assert_not_nil @user.clocks.includes(:users).order("users.name").find(@clock.id)
end
should "find a clock with users and runs order by users.name" do
assert_not_nil @user.clocks.includes(:users, :runs).order("users.name").find(@clock.id)
end
should "find a clock with users, runs and user_runs order by users.name" do
assert_not_nil @user.clocks.includes(:users, :runs => :user_runs).order("users.name").find(@clock.id)
end
end
每次测试,但最后一次测试通过。这不是一个错误吗?
答案 0 :(得分:0)
不应该是
user.clocks.find_by_id(params[:id], :include => [:users, {:runs => :user_runs}])
答案 1 :(得分:0)
我相信这是一个错误,所以我在此报告:https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5768