17831

Rails Find_by_id raises exception

I was led to believe that the difference between Object.find and Object.find_by_id is that find will raise a <strong>RecordNotFound</strong> exception whereas find_by_id simply returns nil if nothing is found.

However, in my Rails 3 app if I attempt to search my Uploads model with a bogus id I get:

<strong>ActiveRecord::RecordNotFound in UploadsController#show</strong>

Couldn't find Upload with id=59

<strong>Request</strong>

<strong>Parameters:</strong>

{"id"=>"59"}

Here is the line of code thats messing up:

@upload = Upload.find_by_id(params[:id])

I'm using Rails 3.1.3.

Answer1:

To throw the 404 error, it needs to be

Upload.find_by_id!(params[:id])

The exclamation point is the magic.

Answer2:

This turned out to be a problem with the Impressionist gem that I'm using as it was hooked into my Upload show action and tried to execute it's own find before I had a chance to deal with it.

Recommend

  • git push: expected committer name/email
  • Unicode characters not showing in Zend_Pdf?
  • How to work on same github repo from two systems?
  • how to store url image in cache memory then display in imageview in iphone
  • d3js: time scaling and “1901”
  • angularjs ng-bind-html html input part missing
  • Slow performance in hybrid AngularJS and Angular application in Safari
  • Efficient User-Agent Regex to find Safari in Python
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Typescript - Unable to get 'import' statement to function
  • Google Custom Search with transparent background
  • Read a local file using javascript
  • Insert into database using onclick function
  • What is Eclipse's Declaration View used for?
  • req.body is undefined - nodejs
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Running a C# exe file
  • Can I make an Android app that runs a web view in Chrome 39?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Symfony2: How to get request parameter
  • jquery mobile loadPage not working
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • coudnt use logback because of log4j
  • How to stop GridView from loading again when I press back button?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Running Map reduces the dimensions of the matrices
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?