2

I have just started to get a rather strange issue with my rails 3.2.18 app

I am using authentication with cookies as described on Railscasts. For the past year (until I upgraded to 3.2.18) all has been well.

However I now have the following issue. I can log on successfully, and navigate to a few pages. However, after an undetermined number of page clicks I get a 500 internal page error. In my production log I see the following error (I have changed the actual token code)

ActiveRecord::RecordNotFound (Couldn't find User with auth_token = XXXXXXXXXXXXXX):
  app/controllers/application_controller.rb:28:in `current_user'

When I look at the database entry for that user I see a correct auth_token entry. The line of code that is being referenced in the application controller is

@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]

So yes, need to replce this witth a where rather than find_by statement for things to woork in Rails 4, but this, I don't think, should be causing the issue, it is also odd that the inital authentication and then a few pages clicks work, and then it doesn't work.

To be able to login in again I have to shutdown the browser session (I have the same on firefox, safari, and IE) and then restart and I can login for a period of time.

In addition I do not get this issue in development, only in production

Michael Moulsdale
  • 1,488
  • 13
  • 34

1 Answers1

2

Well in the end I changed the find_by query to a where query as follows

Original

@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]

New

@current_user ||= User.where("auth_token =?", cookies[:auth_token]).first if cookies[:auth_token]

And this seems to have resolved things. I thought that find_by was depricated in Rails 4, but obviously something was not totally happy.

Michael Moulsdale
  • 1,488
  • 13
  • 34
  • We are having exactly the same problem recently in rails 3.2.12. Do you know what caused the problem? – user938363 Jul 03 '14 at 04:29
  • Did you try to use find_by_auth_token()? (without !) – user938363 Jul 03 '14 at 04:30
  • I didn't try w/o the bang (!), was happier user the where query. And I didn't really find the cause, but was happy with moving to the where statement which fixed the issue, and is better for moving to Rails 4 – Michael Moulsdale Jul 03 '14 at 12:03