在 gem 中覆盖模型,添加回调和方法 [英] Overriding model in gem, adding callback and methods

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

问题描述

我已经在我的应用上安装了 ActiveRecord 信誉系统.我将如何覆盖或添加回调/方法到 评估模型?

一般来说,您如何为您安装的 gem 添加到任何模型中?

解决方案

只需重新打开类:

模块 ReputationSystem类评价<活动记录::基础def my_method_here耶!"结尾结尾结尾

您可以将此文件放在 config/initializers/my_monkey_patch.rblib/my_monkey_patch.rb 中,但后者必须加载到您的代码中.p>

I've installed the ActiveRecord Reputation System on my app. How would I go about overriding or adding a callback/method to Evaluation model?

In general how do you add to any model for a gem you installed?

解决方案

Simply reopen the class:

module ReputationSystem
  class Evaluation < ActiveRecord::Base
    def my_method_here
      puts "Yey!"
     end
   end
end

You can put this file in config/initializers/my_monkey_patch.rb or in lib/my_monkey_patch.rb, but the later must be loaded into your code.

这篇关于在 gem 中覆盖模型,添加回调和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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