Rails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

后端 未结 8 1469
無奈伤痛
無奈伤痛 2020-11-28 12:03

I am using Rails 2.3.5 and in that if I give Model.find(1) and if 1 is not in the database, it returns ActiveRecord error. Should it just be returning nil

8条回答
  •  盖世英雄少女心
    2020-11-28 12:28

    Rails 4 Method

    if user = User.find_by(id: params[:id]) 
      #do something with user
    else
      #throw error or redirect
      raise ActiveRecord::RecordNotFound
    end
    

提交回复
热议问题