ActiveRecord :: RecordNotFound - 引发了哪个类?

时间:2015-12-16 03:13:37

标签: ruby-on-rails activerecord

如果我的控制器中有以下代码:

def update
  user = User.find(params[:user_id])
  house = House.find(params[:house_id])
rescue ActiveRecord::RecordNotFound
  # How do I know whether finding user or finding house raised the exception?
end

我如何知道哪个find引发了异常?

1 个答案:

答案 0 :(得分:0)

在错误实例上似乎没有任何方法可以确定 引发了错误。

您可以这样做:

find_by_id

或者,您可以使用def update user = User.find_by_id(params[:user_id]) person = Person.find(params[:person_id]) house = House.find_by_id(params[:house_id]) do_something if user.nil? || person.nil? do_something_else if house.nil? end

  function myFunction(){
      document.getElementById("myImg").src = "people5.png";
    };
    window.onload = myFunction;