因此,我尝试根据在另一个下拉菜单中选择的客户来填充显示某个客户项目的下拉菜单。我正在使用javascript,在Project控制器中调用函数“byProjectId”。
$("#customer_id").bind( "change", function(){
$("#task_project_id").empty();
$.ajax({
type: 'GET',
url: '/projects/byCustomerId/' + $(this).val(),
dataType: 'script'
})
});
我的byCustomerId函数如下:
def byCustomerId
logger.debug "DEBUGGING HERE"
if params[:id].present?
@projects = Customer.find(params[:id]).projects.order(:name)
else
@projects = []
end
respond_to do |format|
format.js
end
end
所以我所做的就是根据传递的Customer ID值检索Projects列表。但是,当我实际执行此操作时,在实际执行此方法中的代码之前会进行一些额外的数据库调用。这是运行rails服务器的控制台的输出:
Started GET "/projects/byCustomerId/5?_=1342642985360" for 127.0.0.1 at 2012-07-
18 15:23:05 -0500
Processing by ProjectsController#byCustomerId as JS
Parameters: {"_"=>"1342642985360", "id"=>"5"}
←[1m←[36mMember Load (1.0ms)←[0m ←[1mSELECT "members".* FROM "members" WHERE
"members"."id" = 7 LIMIT 1←[0m
←[1m←[35mProject Load (0.0ms)←[0m SELECT "projects".* FROM "projects" WHERE "
projects"."id" = 5 LIMIT 1
Completed in 69ms
ActiveRecord::RecordNotFound (Couldn't find Project with ID=5):
这表明它正在尝试询问ID为5的所有PROJECTS,即使5是它应该要求的客户ID。当我使用同样恰好是有效ProjectID的不同CustomerID时,它会在此之后立即运行正确的调用,但不会发生RecordNotFound异常:
DEBUGGING HERE
←[1m←[36mCustomer Load (0.0ms)←[0m ←[1mSELECT "customers".* FROM "customers"
WHERE "customers"."id" = 1 LIMIT 1←[0m
←[1m←[35mProject Load (1.0ms)←[0m SELECT "projects".* FROM "projects" WHERE (
"projects".customer_id = 1) ORDER BY name
“DEBUGGING HERE”是我在byCustomerId函数中的代码之前实现这些调用的唯一方法。如果我传递的客户ID也作为项目ID有效,它将返回正确的数据并正确填充,只是对不正确的sql调用中的数据执行任何操作,但是当客户ID与项目ID不匹配时,这些对数据库的额外调用会获得异常,并且不会填充任何内容,即使后面的查询仍然没有问题。我无法弄清楚这些额外的数据库调用来自哪里?我只是通过向Project控制器发送任何数据或其他东西来调用它们吗?