[英]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
命令,我会尝试这些步骤(虽然还没有测试过):
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.generate
command should run.generate
命令。 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
.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
。versions
table names to your custom table names inside them.versions
表名称更改为其中的自定义表名称。 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).create_versions
迁移文件并将其重命名回其原始名称(还原对此文件的更改)。 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.