I'm using PaperTrail 4.1 with Rails 4.2.
I have defined several custom methods in an initializer (see: How to add a method to the versions model of Paper_trail?)
#config/initializers/paper_trail.rb
PaperTrail::Rails::Engine.eager_load!
module PaperTrail
class Version < ActiveRecord::Base
scope :scoped, lambda { #selects some records }
def custom_method
#does some stuff
end
end
end
Every so often in development environment I get a method not defined error
for methods/ scopes defined in this initializer.
Restarting the server fixes the problem.
Why are these methods being 'lost' to Rails?
Is this an issue that will also present itself in production or other environments?
What steps can I take to find the cause of this issue?
For anyone else arriving here, apparently this is a known issue with PaperTrail
From https://github.com/airblade/paper_trail/pull/492
Now the paper_trail source get's reloaded in the development environment when saving a file which means the class gets discarded from the cache and rebuild from the paper_trail sources. The initializer is not interpreted again since they are one time only, no module_eval, no abstract class -> exceptions.
And a fix has been included in the latest version of the gem: https://github.com/airblade/paper_trail/pull/557
In essence, it is no longer advised to use an initializer to add custom methods to PaperTrail, and instead to use a model that inherits from PaperTrail (which is a much better fit with AR).
# app/models/paper_trail/version.rb
module PaperTrail
class Version < ActiveRecord::Base
include PaperTrail::VersionConcern
# my custom methods
end
end