简体   繁体   中英

Different table name for Paper Trail?

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):

  • You already have a migration with the name 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.
  • Then the generate command should run. It should install a few files, their names will be printed out to the console.
  • Now open the newly generated 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, eg post_versions .
  • Open all other generated migrations and change the versions table names to your custom table names inside them. There is no need to rename these files though.
  • Now go back to your original (and now temporarily renamed) create_versions migration file and rename it back to its original name ( revert the changes on this file).
  • Try to run the migrations! It should work now.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM