75192

Troubleshooting friendly_id w/ Rails 4: working as expected in rails console, getting RecordNotFound

Question:

I've been successfully using friendly_id with multiple models in my Rails 4 app. I previously had two models that I've decided to combine (Terminals and Locations). The Terminals show action was previously working with friendly_id, where I could visit /terminals/albany and get the Terminal with the slug 'albany'.

I've gone through the bulk of the refactor to condense down to just Locations, and am on the last piece (fixing views). Along the way I've been testing things in Rails Console and using rspec, and things seem to work as expected. There are other models that still work as expected as well.

Now that I'm working on the new show page for Locations, I'm getting the following error when attempting to visit /locations/albany:

ActiveRecord::RecordNotFound in LocationsController#show Couldn't find Location with id=albany

However, when I visit /locations/5, it successfully loads the record. It also works in Rails Console:

Location.find(5) # returns the correct location object Location.find(5).slug # returns 'albany' Location.friendly.find('albany') # returns the correct location object

When I look at the generated slugs in Rails dbconsole, they all look like they were generated correctly and there are no duplicates.

Here's my model:

Class Location < ActiveRecord::Base extend FriendlyId friendly_id :slug_candidates, use: [:slugged, :history] def slug_candidates [ :name, [:name, :zip], [:name, :zip, :location_type], ] end end

And here's my show action in my controller:

def show @locations = Location.friendly.find(params[:id]) end

I'm stumped! Any hints?

Answer1:

have you tried adding :finders?

friendly_id :slug_candidates, use: [:slugged, :history, :finders]

Recommend

  • Unable to find bundled Java version on Flutter
  • UITableView superClass for delegate?
  • How to make this boolean true? [closed]
  • Android - 9 patch
  • Set RippleDrawable corner radius programmatically
  • conditional formatting entire row if cell contains text
  • alternative to audioContext.copyToChannel() in Safari and Edge
  • How should comments for interface and class methods be different
  • C - Freeing memory after strdup()
  • Detect failed tasks in concurrent.futures
  • What's with these g++ “multiple definition” errors?
  • php : email sending failed with more than one attachment
  • .Net FtpWebRequest fails sometimes
  • How to call procedure in kohana 3.1
  • Can you recommend a good php Gantt charting development?
  • Query URL for Log In Status, javascript
  • How can converting a pointer to `void*` twice be invalid?
  • Symfony 1.4 compatible I18N translation system?
  • Velocity (VM) template request parameters: Getting GET variables
  • date changes on export kendoGrid
  • Passing HTTP Post from AWS API GW to Lambda
  • Is there a .NET method equivalent to NETWORKDAYS in Excel?
  • How to run Daphne Server (Django Channels) & workers in the background?
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • How to use array in autohotkey?
  • Generate and export point cloud from Project Tango
  • getting the class name of an HTML tag using XPATH
  • Creating 2d platforms using JavaScript
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • How to write seo friendly url's using htaccess?
  • concise way of flattening multiindex columns
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Python 3x- Compression Makes File Bigger :(
  • How to turn off notice reporting in xampp?
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • How to check if object is null in Java?