Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord::RecordNotFound when validations fail #148

Closed
fschwahn opened this issue Sep 5, 2011 · 6 comments
Closed

ActiveRecord::RecordNotFound when validations fail #148

fschwahn opened this issue Sep 5, 2011 · 6 comments

Comments

@fschwahn
Copy link
Contributor

fschwahn commented Sep 5, 2011

Hi,
I'm using the following versions:

rails (3.1.0)
friendly_id (4.0.0.beta10)
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.8.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03

I have a model like this:

class Picture < ActiveRecord::Base
  extend FriendlyId

  friendly_id :name, :use => :slugged

  # ...

  private
    def normalize_friendly_id(text)
      text.to_slug.normalize! :transliterate => :german
    end

end

When I edit this model and change the name (which is used for the slug), and a validation fails, I am correctly redirected back to the form. However, when I send the form again, a ActiveRecord::RecordNotFound-exception is raised.

Thanks for the great gem!

@norman
Copy link
Owner

norman commented Sep 5, 2011

The error is happening across two requests, and I'm not sure what your form does, or what parameters it sends. If you can show me more explicitly the steps to reproduce the problem, I'd be happy to look into it but from the information you've given so far I suspect it may be best to debug your application a bit more to figure out if FriendlyId is really the problem, and if so, how and why.

@fschwahn
Copy link
Contributor Author

fschwahn commented Sep 5, 2011

I reproduced the bug in a minimal app, I can upload it if you like.
I think the problem is that I change the field (e.g. title) from which the slug is created, but a validation fails, and thus the updated title and slug aren't saved to the database. But when the form is re-submitted friendly_id will do the lookup using the new slug which hasn't been persisted yet. The error message I get if I change the title to lala is:

Couldn't find Post with id=lala

In my minimal app the form is a vanilla rails scaffolded form.

@norman
Copy link
Owner

norman commented Sep 5, 2011

Sure if you could upload the app somewhere or email it to norman@njclarke.com that would be great. Thanks!

@fschwahn
Copy link
Contributor Author

fschwahn commented Sep 5, 2011

You can find the app here: https://github.com/fschwahn/friendly_id_issue_148
The instructions to get it running are inside the README.

norman added a commit that referenced this issue Sep 6, 2011
@norman
Copy link
Owner

norman commented Sep 6, 2011

Thanks a lot for the help reproducing the bug. I've added a failing test for the issue and will get a fix in as soon as possible.

@norman norman closed this as completed in d14a0a7 Sep 7, 2011
@fschwahn
Copy link
Contributor Author

fschwahn commented Sep 7, 2011

Thanks for fixing it! I just tested it was my app and it works now.

parndt pushed a commit that referenced this issue May 20, 2018
This ensures that we use the new value of a slug when calling
`to_param` within an ActiveRecord callback. It also seems to pass all
the other regression tests I've added, thus keeping the fixes working
for #259 and #148, and as a bonus, prevents getting a bunch of
deprecation warnings in Rails 5.1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants