为什么在这个Rails方法中无法获救ActiveRecord :: StatementInvalid?

时间:2012-07-14 13:01:16

标签: ruby-on-rails exception activerecord rescue

为什么我不能用以下方法拯救任何东西?

def get_things
  begin
    things= @member.things.where("id>?",params[:id])
  rescue ActiveRecord::StatementInvalid
    render( inline: "RESCUED ActiveRecord::StatementInvalid" )
    return
  rescue
    render( inline: "RESCUED something" )
    return
  end
  render( inline: "#{things.first.title}" )
end

使用有效ID调用时,它可以工作:

$  curl -vd "id=3" http://localhost:3000/get_things

但如果我错了一个,例如:

$  curl -vd "id=3,0" http://localhost:3000/get_things
$  curl -vd "id='3'" http://localhost:3000/get_things

异常未获救:

< HTTP/1.1 500 Internal Server Error
<h1>
  ActiveRecord::StatementInvalid
    in ApplicationController#get_things
</h1>
<pre>PG::Error: ERROR:  invalid input syntax for integer: &quot;'3'&quot;

仅在渲染发生在开始/救援块

内时
def get_things
  begin
    things= @member.things.where("id>?",params[:id])
    render( inline: "#{things.first.title}" )
  rescue ActiveRecord::StatementInvalid
    render( inline: "RESCUED ActiveRecord::StatementInvalid" )
    return
  end
end

它按预期工作:

$ curl -vd "id='3'" http://localhost:3000/get_things
  < HTTP/1.1 200 OK
  RESCUED ActiveRecord::StatementInvalid

2 个答案:

答案 0 :(得分:7)

据我所知,您的案例中的things将是一个包含查询信息的类,但在您尝试根据查询访问元素之前,查询将不会执行(如{ {1}})。

things.first

这就是无法获救的原因,因为在渲染行中发生异常,而不是在创建things= @member.things.where("id>?",params[:id]) # query not run things= things.order("id desc") # still not run things.first.title # now the query runs, the statement can be invalid

这应该没关系:

things

答案 1 :(得分:-1)

您可以将参数更改为int:

params[:id] = params[:id].to_i if params[:id].present?
things= @member.things.where("id>?",params[:id])

或者您可以在config/routes.rb中为params添加验证器:

resources :things, :constraints => {:id => /\d+/}