Is it possible to specify a different table name (other than versions
) for the PaperTrail gem?
In my Rails app, I already have a Versions model/table, which has nothing to do with active record versioning (my app let's uses fork a "prototype" and for better or worse I used "version" as a label for these forks). It is quite pervasive through my app, and I don't want to rename this.
When running bundle exec rails generate paper_trail:install
, I get Migration already exists: create_versions
.
Basically, I would like the table to be PaperTrailVersions
along with the methods to access the trail to be similarly namespaced.
Any ideas? Or should I just use the Audited gem which uses a audits
table?
PaperTrail supports custom version classes which can have custom table names defined.
class PostVersion < PaperTrail::Version
self.table_name = :post_versions
end
class Post < ActiveRecord::Base
has_paper_trail :class_name => 'PostVersion'
end
As of the failed generate
command, I would try these steps (haven't tested them though):
CreateVersions
because you already have a versions
table. That is why the generate
command fails - it cannot create a migration with the same name. I think that you can simply temporarily rename the old migration (for your original versions
table migration). You just need to rename the file and the classname inside the file.generate
command should run. It should install a few files, their names will be printed out to the console.create_versions
migration file and rename it as well as the class name inside from CreateVersions
to a name according to your custom versions table name, such as CreatePostVersions
. Also rename any mention of the versions
table inside it to your custom table name, e.g. post_versions
.versions
table names to your custom table names inside them. There is no need to rename these files though.create_versions
migration file and rename it back to its original name (revert the changes on this file).The steps may seem cumbersome but they just temporarily rename the old migration to something else so that the generation command can run. Then you just need to change the table name inside the generated migration to the new table name.
The files that will be generated with the generate
command can be seen here in the source code. These are the files that you'll need to modify.