Search code examples
ruby-on-railsherokuruby-on-rails-4paper-trail-gem

Heroku can't find user but works in dev


I'm hitting a problem with my heroku deployment. Here is the log:

   2014-04-29T16:41:59.782999+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
    2014-04-29T16:41:59.782933+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
    2014-04-29T16:41:59.804819+00:00 app[web.1]: 
    2014-04-29T16:41:59.804823+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
    2014-04-29T16:41:59.804826+00:00 app[web.1]:   app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804824+00:00 app[web.1]:   config/initializers/paper_trail.rb:4:in `user'
    2014-04-29T16:41:59.804854+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804828+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `each'
    2014-04-29T16:41:59.804856+00:00 app[web.1]:   app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
    2014-04-29T16:41:59.804869+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `each'
    2014-04-29T16:41:59.804871+00:00 app[web.1]:   app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
    2014-04-29T16:41:59.804872+00:00 app[web.1]:   app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
    2014-04-29T16:41:59.804875+00:00 app[web.1]: 
    2014-04-29T16:41:59.804874+00:00 app[web.1]: 
    2014-04-29T16:41:59.804857+00:00 app[web.1]: 
    2014-04-29T16:41:59.804858+00:00 app[web.1]: 
    2014-04-29T16:41:59.804864+00:00 app[web.1]: 
    2014-04-29T16:41:59.804865+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
    2014-04-29T16:41:59.804866+00:00 app[web.1]:   config/initializers/paper_trail.rb:4:in `user'
    2014-04-29T16:41:59.804868+00:00 app[web.1]:   app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'

The offending initializer is as follows:

module PaperTrail
  class Version < ActiveRecord::Base
    def user
      User.find self.whodunnit.to_i
    end

    def nextversion
        self.next
    end
  end
end

Which is getting called in the _history view with the following:

<%= gravatar_for version.user %><%= link_to  version.user.username, user_path(version.user) %>

This works fine in development and appears to work fine in production on most edit pages but on one I am encountering this error. I wanted to check the version in the console but it seems I can't access paper_trail Versions in the console at all, I can only assume this is because I don't have a Version model, only a table and a controller.

How can I fix this?


Solution

  • You are getting following error

    ActiveRecord::RecordNotFound (Couldn't find User with id=0)

    on the following method

    def user
      puts "Value of whodunnit is -----------> #{self.whodunnit}"
      User.find self.whodunnit.to_i
    end
    

    which is called in

    <%= gravatar_for version.user %><%= link_to  version.user.username, user_path(version.user) %>
    

    because self.whodunnit.to_i is returning 0. That means, whodunnit is either "" or nil or 0. Please set the correct value of whodunnit attribute. Also, you can check the current value of whodunnit by adding a logger or puts statement in PaperTrail::Version# user method.