简体   繁体   English

Paper Trail 的表名不同?

[英]Different table name for Paper Trail?

Is it possible to specify a different table name (other than versions ) for the PaperTrail gem?是否可以为PaperTrail gem 指定不同的表名(除了versions )?

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).在我的 Rails 应用程序中,我已经有一个版本模型/表,它与活动记录版本控制无关(我的应用程序让我们使用 fork 一个“原型”,无论好坏,我使用“version”作为这些 fork 的标签) . 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 .当运行bundle exec rails generate paper_trail:install ,我得到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.基本上,我希望表是PaperTrailVersions以及访问路径的方法具有类似的命名空间。

Any ideas?有任何想法吗? Or should I just use the Audited gem which uses a audits table?或者我应该只使用使用audits表的审计宝石?

PaperTrail supports custom version classes which can have custom table names defined. PaperTrail 支持可以定义自定义表名称的自定义版本类

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):对于失败的generate命令,我会尝试这些步骤(虽然还没有测试过):

  • You already have a migration with the name CreateVersions because you already have a versions table.您已经有一个名为CreateVersions的迁移,因为您已经有一个versions表。 That is why the generate command fails - it cannot create a migration with the same name.这就是generate命令失败的原因 - 它无法创建具有相同名称的迁移。 I think that you can simply temporarily rename the old migration (for your original versions table migration).我认为您可以简单地暂时重命名旧迁移(对于您的原始versions表迁移)。 You just need to rename the file and the classname inside the file.您只需要重命名文件文件内的类名。
  • Then the generate command should run.然后应该运行generate命令。 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 .现在打开新生成的create_versions迁移文件,将它以及CreateVersions的类名重命名为根据您的自定义版本表名称的名称,例如CreatePostVersions Also rename any mention of the versions table inside it to your custom table name, eg post_versions . post_versions提及的versions表重命名为您的自定义表名称,例如post_versions
  • Open all other generated migrations and change the versions table names to your custom table names inside them.打开所有其他生成的迁移并将versions表名称更改为其中的自定义表名称。 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).现在返回到您的原始(现在暂时重命名) create_versions迁移文件并将其重命名回其原始名称(还原对此文件的更改)。
  • 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.将与被生成的文件generate命令可以看出这里在源代码中。 These are the files that you'll need to modify.这些是您需要修改的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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