friendly_id icon indicating copy to clipboard operation
friendly_id copied to clipboard

Works in console, not in controller

Open Svashtar opened this issue 11 years ago • 14 comments

Hi,

i am pretty sure i am doing things right, but something is off. I have this in controller:

@location = Location.friendly.find(params[:id])

Model:

extend FriendlyId
friendly_id :name, use: :slugged

And the error says:

Couldn't find Location with 'id'=my-super-name
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"

Yet, in console i get record just fine like this:

Location.friendly.find('my-super-name')

Controller works when using

friendly_id :name, use: [:slugged, :finders]

But i think it should work without it, too, as i am doing a friendly.find. :finders, as expected also work without friendy.find. - but i would like to control this...

I am on Rails 4.2.1 - Ruby 2.1.2 I have already searched around, but only found this one single Stackoverflow topic without proper answer to solve this.

Thx

Svashtar avatar May 22 '15 12:05 Svashtar

Did you restart the server after adding FriendlyId? Try printing params.inspect in the log or with puts to see what the id in the controller is, any chance there's a space? On Fri, May 22, 2015 at 09:30 Mitja Čebokli [email protected] wrote:

Hi,

i am pretty sure i am doing things right, but something is off. I have this in controller:

@location = Location.friendly.find(params[:id])

Model:

extend FriendlyId friendly_id :name, use: :slugged

And the error says:

Couldn't find Location with 'id'=my-super-name raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"

Yet, in console i get record just fine like this:

Location.friendly.find('my-super-name')

Controller works when using

friendly_id :name, use: [:slugged, :finders]

But i think it should work without it, too, as i am doing a friendly.find. :finders, as expected also work without friendy.find. - but i would like to control this...

I am on Rails 4.2.1 - Ruby 2.1.2 I have already searched around, but only found this one single Stackoverflow http://stackoverflow.com/questions/20314927/rails-friendly-id-couldnt-find-category-with-id-electronics topic without proper answer to solve this.

Thx

— Reply to this email directly or view it on GitHub https://github.com/norman/friendly_id/issues/666.

norman avatar May 22 '15 12:05 norman

Restarted server numerous times :) Parameter comes through fine.

{"id"=>"my-super-name"}

Looking through the log i've found the difference on select itself. Finders:

Location Load (1.1ms)  SELECT  `locations`.* FROM `locations` WHERE `locations`.`slug` = 'my-super-name'  ORDER BY `locations`.`id` ASC LIMIT 1

No finders (both time using friendly.find):

Location Load (1.0ms)  SELECT  `locations`.* FROM `locations` WHERE `locations`.`id` = 0 LIMIT 1

Svashtar avatar May 22 '15 13:05 Svashtar

What version of FriendlyId are you using? On Fri, May 22, 2015 at 10:13 Mitja Čebokli [email protected] wrote:

Parameter comes through fine.

{"id"=>"my-super-name"}

Looking through the log i've found the difference on select itself. Finders:

Location Load (1.1ms) SELECT locations.* FROM locations WHERE locations.slug = 'my-super-name' ORDER BY locations.id ASC LIMIT 1

No finders (both time using friendly.find):

Location Load (1.0ms) SELECT locations.* FROM locations WHERE locations.id = 0 LIMIT 1

— Reply to this email directly or view it on GitHub https://github.com/norman/friendly_id/issues/666#issuecomment-104656226.

norman avatar May 22 '15 13:05 norman

friendly_id 5.1.0

Svashtar avatar May 22 '15 13:05 Svashtar

maybe try taking out finders?

friendly_id :name, use: [:slugged]

pavankat avatar May 25 '15 03:05 pavankat

No, that's the whole point > friendly.find doesn't work without :finders. But with :finders, normal find works as friendly.find. No good.

Svashtar avatar May 25 '15 08:05 Svashtar

Would love to help with this, but without having your application code in front of me there's little I can do. If you can provide a sample app that reproduce there error I'd be more than happy to look into it.

norman avatar Jun 01 '15 19:06 norman

Also seeing the same issue on rails 4.2.1

mesge avatar Jul 10 '15 04:07 mesge

Seem to have the same issues as well in 4.2.1 @mesge any luck?

nickyrasch avatar Aug 12 '15 22:08 nickyrasch

Unfotunately not @nickyrasch. As a work around, I took this advice and removed finders. find now works as friendly.find.

mesge avatar Aug 13 '15 00:08 mesge

Look... using version '5.0.3' the error was "undefined method `primary' for #ActiveRecord::ConnectionAdapters::Column:0x000000093ce2c8. I modified it "Room.friendly.find(params[:id])" for this "Room.friendly.find_by(id: params[:id])", and work.

Using version "5.1.0" the error is "raise RecordNotFound". I modified the same thing, but one functionality broken. So i recommend use version "5.0.3".

jrgvf avatar Aug 16 '15 03:08 jrgvf

Looks like this issue is staled and can be closed. Xxx.friendly.find works good both in controller and console by default. So, this problem should be somewhere else, but not in friendly_id itself. But, please, fill free to open issue again if you find additional details to clarify problem.

kimrgrey avatar Jan 08 '16 12:01 kimrgrey

This issue is closed. However, I had the same problem and the answer was that friendly doesn't seem to work with "load_and_authorize_resource".

Big-Gui avatar Aug 20 '17 18:08 Big-Gui

This issue is closed. However, I had the same problem and the answer was that friendly doesn't seem to work with "load_and_authorize_resource".

YOU SAVED ME!!!

IsmaelEzequiel avatar Apr 17 '19 13:04 IsmaelEzequiel