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

How to solve has_paper_trail method missing in rails 3.2 #789

Closed
dukex opened this issue May 2, 2016 · 10 comments
Closed

How to solve has_paper_trail method missing in rails 3.2 #789

dukex opened this issue May 2, 2016 · 10 comments
Milestone

Comments

@dukex
Copy link

dukex commented May 2, 2016

This issues is how to solve a problem, please create a pull request to fix it or update the documentation.

Using rails 3.2 and paper_trail 5.0.0 I receive the following error:

.../activerecord-3.2.19/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined local variable or method `has_paper_trail' for #<Class:0x007f339a7baaf0> (NameError)

To fix it, create an initiailizer and load the active record framework:

$ echo "require 'paper_trail/frameworks/active_record'" > config/initializers/paper_trail.rb

The problem was introducted by 2029ea3 in #725

@jaredbeck
Copy link
Member

Are you using require: false in your Gemfile? For example:

gem "paper_trail", require: false

Can you provide a complete stack trace for the NameError? Thanks.

@dukex
Copy link
Author

dukex commented May 3, 2016

The Trace:

/home/duke/.gem/ruby/2.1.3/gems/activerecord-3.2.19/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined local variable or method `has_paper_trail' for #<Class:0x007faa7138
f2e8> (NameError)
        from /src/xproject/app/models/system.rb:140:in `<class:System>'
        from /src/xproject/app/models/system.rb:90:in `<top (required)>'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:469:in `load'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:469:in `block in load_file'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:639:in `new_constants_in'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:468:in `load_file'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:353:in `require_or_load'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:502:in `load_missing_constant'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:192:in `block in const_missing'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:190:in `each'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:190:in `const_missing'
        from /src/xproject/config/initializers/rails_admin.rb:93:in `block in <top (required)>'
        from /home/duke/.gem/ruby/2.1.3/bundler/gems/rails_admin-992d962d65f2/lib/rails_admin.rb:29:in `call'
        from /home/duke/.gem/ruby/2.1.3/bundler/gems/rails_admin-992d962d65f2/lib/rails_admin.rb:29:in `config'
        from /src/xproject/config/initializers/rails_admin.rb:4:in `<top (required)>'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `load'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `block in load'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:245:in `load'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/engine.rb:593:in `block (2 levels) in <class:Engine>'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/engine.rb:592:in `each'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/engine.rb:592:in `block in <class:Engine>'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/initializable.rb:30:in `instance_exec'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/initializable.rb:30:in `run'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/initializable.rb:54:in `each'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/initializable.rb:54:in `run_initializers'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/application.rb:136:in `initialize!'
        from /home/duke/.gem/ruby/2.1.3/gems/railties-3.2.19/lib/rails/railtie/configurable.rb:30:in `method_missing'
        from /src/xproject/config/environment.rb:6:in `<top (required)>'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:251:in `require'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:251:in `block in require'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:236:in `load_dependency'
        from /home/duke/.gem/ruby/2.1.3/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:251:in `require'
        from /home/duke/.gem/ruby/2.1.3/gems/spring-1.6.4/lib/spring/application.rb:92:in `preload'
        from /home/duke/.gem/ruby/2.1.3/gems/spring-1.6.4/lib/spring/application.rb:143:in `serve'

My Gemfile:

gem 'paper_trail' # implementa auditoria para os modelos

@batter
Copy link
Collaborator

batter commented May 3, 2016

Are you declaring has_paper_trail at class or instance level? Can you share your System model?

@jaredbeck
Copy link
Member

jaredbeck commented May 3, 2016

Given that your Gemfile requires PT, here's what I expect to happen:

application.rb loads your bundle, which calls `require "paper_trail"`
  paper_trail.rb requires paper_trail/has_paper_trail.rb which defines `PaperTrail::Model`
  paper_trail.rb requires paper_trail/frameworks/active_record.rb
    active_record.rb defines an `on_load` callback to `include PaperTrail::Model`
the `on_load` callback happens
finally, your environment.rb is run

This is based on my vague understanding of the rails boot process, and could be totally wrong, or could have changed between rails 3 and rails 4.

Our next step should be to reproduce this issue in a brand new rails app without rails_admin. If we can do that, then we can fire up a debugger and see if I'm right about the boot process.

@Skulli
Copy link

Skulli commented May 4, 2016

I am having a similar problem with my rails 4.2.6 setup here.

gems/activerecord-4.2.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined local variable or method `has_paper_trail' for Client (call 'Client.connection' to establish a connection):Class (NameError)
from app/models/client.rb:4:in `<class:Client>'
from app/models/client.rb:3:in `<top (required)>'

@jaredbeck
Copy link
Member

I have reproduced this issue in a new rails app: https://github.com/jaredbeck/pt_issue_789

The problem can be reproduced when a custom initializer autoloads an AR model before the paper_trail.initialisation initializer has run.

@jaredbeck jaredbeck added this to the 5.0.1 milestone May 4, 2016
jaredbeck added a commit that referenced this issue May 4, 2016
@jaredbeck
Copy link
Member

Closed by #794. Sascha and Duke, please try master and let me know if that solves your problem.

jaredbeck added a commit that referenced this issue May 4, 2016
@jaredbeck
Copy link
Member

This fix has been released in 5.0.1.

@dukex
Copy link
Author

dukex commented May 6, 2016

Thank you @jarebeck I'll try this version soon

@Skulli
Copy link

Skulli commented May 7, 2016 via email

devonestes pushed a commit to devonestes/paper_trail that referenced this issue May 9, 2016
devonestes pushed a commit to devonestes/paper_trail that referenced this issue May 9, 2016
devonestes pushed a commit to devonestes/paper_trail that referenced this issue May 9, 2016
devonestes pushed a commit to devonestes/paper_trail that referenced this issue May 9, 2016
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

4 participants