如何在 Paper_trail 的版本模型中添加方法? [英] How to add a method to the versions model of Paper_trail?

查看:85
本文介绍了如何在 Paper_trail 的版本模型中添加方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 paper_trail 版本创建以下方法:

I want to create the following method for paper_trail versions:

def user
  User.find self.whodunnit.to_i
end

这样我就可以在我的应用中自然地访问某个版本的用户,就像它具有 belongs_to 关系一样.

So that I can access a version's user naturally in my app as if it has a belongs_to relation.

为了覆盖/添加到 paper_trail 的版本模型,我应该把这个函数放在哪里(文件夹和文件名)?

推荐答案

你可以把它放在打开 PaperTrail 的初始化程序(例如,config/initializers/paper_trail.rb)中::Version 类:

You could put this in an initializer (e.g., config/initializers/paper_trail.rb) that opens the PaperTrail::Version class:

module PaperTrail
  class Version < ActiveRecord::Base
    def user
      User.find self.whodunnit.to_i
    end
  end
end

您需要确认您的 Version 模型是 PaperTrail::Version;旧版本的 gem 只使用 Version.在这种情况下,只需删除外部模块语句.

You'll want to confirm that your Version model is PaperTrail::Version; older versions of the gem use just Version. In that case, just remove the outer module statement.

您还可以创建一个继承自 Version 的自定义类,并在您的 has_paper_trail 调用中指定.例如(来自自述文件):

You could also create a custom class inheriting from Version, and specify that in your has_paper_trail call. For example (from the README):

class PostVersion < PaperTrail::Version
  # custom behaviour, e.g:
  self.table_name = :post_versions
end

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'
end

这篇关于如何在 Paper_trail 的版本模型中添加方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆